i am friends with the same dilemma that you: Controls brusheless motor with arduino. I have some codes here that works with an engine speed ranging from 0 to 100% as this:
#include <Servo.h>
Servo myservo;
void arm(){
// arm the speed controller, modify as necessary for your ESC
setSpeed(0);
delay(1500); //delay 1 second, some speed controllers may need longer
}
void setSpeed(int speed){
// speed is from 0 to 100 where 0 is off and 100 is maximum speed
//the following maps speed values of 0-100 to angles from 0-180,
// some speed controllers may need different values, see the ESC instructions
int angle = map(speed, 0, 100, 0, 180);
myservo.write(angle);
}
void setup()
{
myservo.attach(2);
arm();
}
void loop()
{
int speed;
// sweep up from 0 to to maximum speed in 20 seconds
for(speed = 0; speed <= 100; speed += 5) {
setSpeed(speed);
delay(1000);
}
// sweep back down to 0 speed.
for(speed = 95; speed > 0; speed -= 5) {
setSpeed(speed);
delay(1000);
}
setSpeed(0);
delay(5000); // stop the motor for 5 seconds
}
but I want to control 4 brushless motors for my quadricoptero: I’m working on sketches:
int motorPin = 8;
int motor2Pin = 6;
int motor3Pin = 4;
int motor4Pin = 2;
#define MINCOMMAND 1000
#define MAXCOMMAND 1600
#define DIRCOMMAND 2000
void setup()
{
Serial.begin(9600);
pinMode(motorPin , OUTPUT);
pinMode(motor2Pin , OUTPUT);
pinMode(motor3Pin , OUTPUT);
pinMode(motor4Pin , OUTPUT);
arm();
}
void arm()
{
analogWrite(motorPin, MINCOMMAND / 8);
analogWrite(motor2Pin, MINCOMMAND / 8);
analogWrite(motor3Pin, MINCOMMAND / 8);
analogWrite(motor4Pin, MINCOMMAND / 8);
}
void loop() {
for(int MC = MINCOMMAND ; MC <= MAXCOMMAND; MC +=10) {
analogWrite(motorPin, MC / 8);
analogWrite(motor2Pin, MC / 8);
analogWrite(motor3Pin, MC / 8);
analogWrite(motor4Pin, MC / 8);
delay(500);
}
int dir,a=10;
Serial.println(" D para direita, E para esquerda F para frente e W para traseira ");
if (Serial.available() >0) { //existem caracteres para ler
dir=Serial.read();
Serial.println(a);
//altera a velocidade consoante o caracter.
}
if (dir == 'a')
{
for(int MC = MAXCOMMAND ; MC <= DIRCOMMAND ; MC +=10)
{
analogWrite(motorPin, MC / 8);
Serial.println(dir);
}
}
if (dir == 's')
{
for(int MC = MAXCOMMAND ; MC <= DIRCOMMAND ; MC +=10)
{
analogWrite(motor2Pin, MC / 8);
Serial.println(dir);
}
}
if (dir == 'w')
{
for(int MC = MAXCOMMAND ; MC <= DIRCOMMAND ; MC +=10)
{
analogWrite(motor3Pin, MC / 8);
Serial.println(dir);
}
}
if (dir == 'd')
{
for(int MC = MAXCOMMAND ; MC <= DIRCOMMAND ; MC +=10)
{
analogWrite(motor4Pin, MC / 8);
Serial.println(dir);
}
}
}
it contains an error in the loop that still can not understand.