Hi everyone.
I am using the Servo.h library to control some ESC's, but I'm running into some problems. For some reason, ESC.writeMicroseconds() will ONLY work in a for loop.
The following code is copied from an online example. Does NOT WORK.
#include <Servo.h>
Servo ESC; // create servo object to control the ESC
int test = 1250;
void setup() {
Serial.begin(115200);
ESC.attach(6,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
delay(6000);
}
void loop() {
Serial.println(test);
Serial.println(millis());
ESC.writeMicroseconds(test);
delay(10);
}
Produces the following output:
1250 //"test"
6009 //millis()
1250
6021
1250
6031
The code with the corresponding for-loop (only the void loop() is changed. EVERYTHING else is identical)
void loop() {
for (int i = 1000; i < 1300; i++) {
ESC.writeMicroseconds(i);
if (i> 1250) {
i -= 1;
}
delay(10);
Serial.println(i);
Serial.println(millis());
}
}
This (after having run through values 1000 to 1246) produces following output:
1247
8589
1248
8599
1249
8610
1250
I have no clue how to solve this, since the Servo.h examples for ESC's show, that you should be able to just use writeMicroseconds() in any loop.
Trying to write a value every 10ms when the Servo.h output operates at 50Hz, i.e. 20ms period, seems a bit odd and certainly not anything I've tried. But the output seems to show that it's doing what you asked so what exactly do you mean by "does NOT WORK"?
Robin2 - Both versions print at the same frequency and the output proves it, so that should not change anything significant. If the buffer is getting choked on the non-working code, it should be choked on the working code
Slipstick - I see your point, but neither Serial.print() or the delay is responsible for the behavior. I have tested with various delays and with or without Serial.print. By "Does not work" I mean: Motor doesn't turn. ESC beeps identically to that of the working code, but the motor doesn't turn as opposed to the working code
If it's any help, this code works as well:
#include <Servo.h>
Servo ESC1;
int tempPWMvalue = 0;
int PWMvalue = 0; // actual value to be sent to the ESC (0-180)
void setup() {
Serial.begin(115200);
ESC1.attach(6, 1000, 2000); //D6, large breakout for test LEFT MOTOR, FIXED
}
void loop() {
if (Serial.available()) {
tempPWMvalue = Serial.parseInt();
if (tempPWMvalue != 0) {
PWMvalue = tempPWMvalue;
}
if (PWMvalue > 180) {
PWMvalue = 180;
}
Serial.print("PWMvalue is: "); Serial.print(PWMvalue); Serial.println("");
}
ESC1.write(PWMvalue);
}
if (tempPWMvalue != 0) {
PWMvalue = tempPWMvalue;
}
Needs to be included since the Serial.available returns 0 after the time-out period. This is filtered so that only the values I enter are sent to the ESC
kristianslot:
ESC beeps identically to that of the working code, but the motor doesn't turn as opposed to the working code
If it's an ESC for a quadcopter, it has to be armed before it accepts commands. In the sketch that 'doesn't work', try putting this at the end of setup():