Let's Make Robots!

ESC issue with Arduino Duemilanove. Simple fix?

I found the following code from mem on the arduino forums (attached below) and am using it to control my ESC for my brushless motor. I have tried editing this code, as well as others, but cannot seem to get my motor to spin. Every time I start running the motor, the motor makes a lot of noise and does not spin. I got it to start spinning once, but the ESC always beeps after a few seconds (spinning or not) and stops trying to run the motor. I think the motor should have more than enough torque to move the two smalI attachments I added to it in the air, as I ripped the motor off of a "Miss Hydrofoam" boat plane and am using a 3 cell lipo. What am I doing wrong? This is my first project involving "servo" control ;)

 // this uses the Arduino servo library included with version 0012

// caution, this code sweeps the motor up to maximum speed !
// make sure the motor is mounted securily before running.

#include <Servo.h>

Servo myservo;

void arm(){
// arm the speed controller, modify as necessary for your ESC
setSpeed(0);
delay(1000); //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(9);
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

 

 

  Thanks in advance!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Dude, you GOTTA tab your code.

Everything within squiggily brackets needs to get tabbed-in one tab, in relation to its squiggily brackets. If you look at the top and bottom of a given routine, the code should visually "walk-in" and "walk-out" as your eyes scan vertically. 

This tabbing does not affect the code (at least not with Arduino) but it makes things SO much easier for other folks (and you too!) to read.

Right where is has a comment, "modify as necessary for your ESC " --this is where you need to modify as necessary for your ESC.

It has been my experience working with these ESC's that it's usually Full for one second, then back to 0. This usually triggers the little "chirp" you get from your motor as well. Your code would simply be:

throttleServo.write(whatever full-on is);
delay(1000); 
throttleServo.write(whatever off position is);
goto main loop

 

Also, I should just mention that the "//or use +50 for 2 sec" thing is not really the best way to do it. If you notice, you have a full, one-second-delay in your for-loop and to speed things up, we switch from taking steps of 5 to steps of 50. If you are using a 0-100 scale, this will not be a ramp at all, instead it will be a off-->1/2-->full  with nothing in between. If your ramps are always going to be longer than 100mS, I would do a single step instead for(speed = 0; speed <= 100; speed++) and then vary the length of the delay within the loop instead to control your ramp speed.

 

One more I notice. I think you may have an overflow problem there as well. Your for-loops (using "<=" instead of "<") is going to count to 105 instead of 100. When this gets sent to your setSpeed routine and mapped, your 180 will overflow to 182 or so. Your servo command may or may not like this. --I can't really remember how the servo command reacts to postions bigger than 180...

Thanks for the fast reply! For that would it be

void arm(){
// arm the speed controller, modify as necessary for your ESC
setSpeed(0);
delay(1000); //delay 1 second, some speed controllers may need longer

for(speed = 0; speed <= 100; speed += 5) {   // or use +50 for 2 sec here compared to 20
setSpeed(speed);
delay(1000);
}

And then just set the speed to full in the loop by adding

setspeed(100);

Or am I still missing code?

I see you have "arm" in its own routine, but it looks like it is just setting the ESC to "off" at startup but I don't see a off-full throttle-off transistion anywhere. This is typically what ESC's need at startup to "arm" them.