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
}
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);
}
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
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.
Nicely done guys! Thanks for input to both of you 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
}
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
}
}
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)
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
}
}