Just want to arm and run motor at a single speed

This sketch arms an ESC (Turnigy Plush 40a) and subsequently operates a brushless motor in a sweep up in speed and a sweep down. I've been trying to figure out how to modify it to arm a then run the motor at a given speed... say at 100 (of 180). Every mod I've tested has resulted in... zipo.

... Guidance would be appreciated.

#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
}

Assuming the program does work:

  • Add setSpeed(90); // 90% of maxat the end of the setup() function, after arm(); and before the closing }
  • remove all code from the loop, keep it empty void loop() {}

Upload and see if that works.

Thank you JML. Tested following and no go.

#include <Servo.h>

Servo myservo;

void arm(){ // arming speed controller
setSpeed(0); //delay for ESC initialization
delay(1000); //delay 1 second
}

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();
setSpeed(90);
}

void loop()
{}

Does the sweep up and down work?

What's the arming sequence really for your ESC? just down as you do here (some require to move the joystick to the bottom and then to the neutral or top)? You need to get that right, may be the sweep you were doing was part of the real arming?

You could also Try with 50 instead of 90 just To ensure it's not out of bound

Usually best to use microseconds than angles

(Please correct your posts and use code tags)

Yes, sweep works both up and down... then motor stops for 5 seconds... then repeats.

Am uncertain about what arming sequence is for Turnigy Plush 40a ESC. From code I gather arming = 0 speed for 1 second.

Tested 50, 60. No spin. Also tried 6 pin. No spin.

Yes, sweep works both up and down..

So try this

#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)
{
  int angle = map(speed, 0, 100, 0, 180);
  myservo.write(angle);
}

void setup()
{
  myservo.attach(9);
  arm();
  for (speed = 0; speed <= 100; speed += 5)
  {
    setSpeed(speed);
    delay(1000);
  }
}

void loop()
{
}

If it works, which it should if sweep works, reduce the delay() parameter

Incidentally, why have the for loop run from 0 to 100 and map it to 0 to 180 when you could just have the for loop run from 0 to 180 ?

Bob... thanks for input. When compiling sketch, there's:

"error: 'speed' was not declared in this scope"

... under: void setup()... the "for (speed = 0; speed <= 100; speed += 5)" line.

Regarding loop running from 0 to 100 and mapping it, that sketch is from public domain---I've been playing with it in an attempt to sort it out for my application. Have no idea why original creator choose to map.

just do for ([color=red]int[/color] speed = 0; ...

Nicely done guys! Thanks for input to both of you :slight_smile: Sketch (below) arms and sweeps turbine up incrementally to what seems full speed.

Tested reducing "delay()" parameter during "setup()"---no spin. Increasing "delay()" parameter---that works correctly according to param defined.

Am going to see if I can figure out how to adjust the speed level. Full speed---as it is---is too much. Will play with code for a while to see if answer arrives. Will post final sketch when solution is found---or, ask you guys for help later today.

Working sketch:

#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)
{
int angle = map(speed, 0, 100, 0, 180);
myservo.write(angle);
}

void setup()
{
myservo.attach(9); // using pin 9 for signal wire to ESC (ESC = Turnigy Plush 40a)
arm();
for(int speed = 0; speed <= 100; speed += 5)
{
setSpeed(speed);
delay(1000); // speed interval during sweep up... can increase interval but not decrease
}
}

void loop()
{
}

FINAL SKETCH - Arming ESC and Running Brushless Motor at Constant Speed

Thanks to J-M-L and UKHeliBob for making this work.


#include <Servo.h>

Servo myservo;

void arm()
{
// arms 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)
{
int angle = map(speed, 0, 100, 0, 180); // 0 = no speed, 100 = full speed
// maps to servo's 0-180 degrees
myservo.write(angle);
}

void setup()
{
myservo.attach(9); // using pin 9 for signal wire to ESC (ESC = Turnigy Plush 40a)
arm();
for(int speed = 0; speed <= 50; speed += 5) // set desired brushless motor speed here...
// "50" = 50% throttle/speed, "100" would = 100%
// change "50" to desired speed
// change "5" to desired degree increments
// ...must test any change to confirm working
{
setSpeed(speed);
delay(1000); // speed interval during sweep up, can increase interval but not decrease
}
}

void loop() // nothing in loop
{
}

FINAL SKETCH

Oh, good. No more posts of code without code tags.

What's a "code tag" (?) Paul and I'll gladly attend.

discseven:
What's a "code tag" (?) Paul and I'll gladly attend.

http://forum.arduino.cc/index.php?topic=97455.0

See, specifically, item #6.

:grin: :grin:

Or - there is also at the top of the forum something called How to use this forum - please read. see point 7. If you are posting code or error messages, use "code" tags

( you are not the first one nor the only one not having read that)

FINAL SKETCH - Arming ESC and Running Brushless Motor at Constant Speed

Thanks to J-M-L and UKHeliBob for making this work.


#include <Servo.h>

Servo myservo;

void arm() // arms 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)
{
  int angle = map(speed, 0, 100, 0, 180); // 0 = no speed, 100 = full speed, maps to servo's 0-180 degrees
  myservo.write(angle);
}

void setup()
{
  myservo.attach(9); // using pin 9 for signal wire to ESC (ESC = Turnigy Plush 40a)
  arm();
  for(int speed = 0; speed <= 50; speed += 5) // set desired brushless motor speed here...
// "50" = 50% throttle/speed, "100" would = 100%
// change "50" to desired speed
// change "5" to desired degree increments
// ...must test any change to confirm working
  {
    setSpeed(speed);
    delay(1000); // speed interval during sweep up, can increase interval but not decrease
  }
}

void loop() // nothing in loop
{
}

Thank you Paul and JML.

FINAL SKETCH - Arming ESC and Running Brushless Motor at Constant Speed

I still don't see the point of using the map() command in this program when you could just write the for loop index value to the ESC.

Bob... if you are interested, edit sketch as you are thinking and I'll upload it and report back. Am encasing turbine in housing so would not be able to access wires until late today or tomorrow.

The only purpose of the setSpeed() function seems to be to allow the use of a range from 0 to 100 when setting the speed of the motor which represents 0 to 100% but it is not actually necessary.

#include <Servo.h>

Servo myservo;

void arm() // arms speed controller, modify as necessary for your ESC
{
  myservo.write(0);
  delay(1000); // delay 1 second, some speed controllers may need longer
}

void setup()
{
  myservo.attach(9); // using pin 9 for signal wire to ESC (ESC = Turnigy Plush 40a)
  arm();
  for (int speed = 0; speed <= 180; speed += 5) // set desired brushless motor speed here...
  {
    myservo.write(speed);
    delay(1000); // speed interval during sweep up, can increase interval but not decrease
  }
}

void loop() // nothing in loop
{
}

UKHeliBob:
The only purpose of the setSpeed() function seems to be to allow the use of a range from 0 to 100 when setting the speed of the motor which represents 0 to 100% but it is not actually necessary.

#include <Servo.h>

Servo myservo;

void arm() // arms speed controller, modify as necessary for your ESC
{
 myservo.write(0);
 delay(1000); // delay 1 second, some speed controllers may need longer
}

void setup()
{
 myservo.attach(9); // using pin 9 for signal wire to ESC (ESC = Turnigy Plush 40a)
 arm();
 for (int speed = 0; speed <= 180; speed += 5) // set desired brushless motor speed here...
 {
   myservo.write(speed);
   delay(1000); // speed interval during sweep up, can increase interval but not decrease
 }
}

void loop() // nothing in loop
{
}

That works Bob. Less is more :slight_smile: Nicely done.

Unless you want to there should be no need to ramp up the speed and certainly no need to do it so slowly.