I've created a simple code to drive a RC electronic speed control (ESC). If I use the code below, I get an output that varies from 0.5ms to 2.5ms. If I comment out the servo.write() and uncomment the servo.writeMicroseconds(), I get the correct span of 1ms to 2ms. Is there something wrong with my code or is there a bug in servo.write()?
Running on Photon 2 with O/S v5.9.0.
Servo myservo; // create servo object to control a R/C servo
// a maximum of eight servo objects can be created
const int PWM_OUTP = D1; // 1ms << out_pulse << 2ms
const int SPD_POT = A0;
STARTUP(WiFi.selectAntenna(ANT_INTERNAL));
// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);
// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);
void setup()
{
(void)logHandler; // Does nothing, just to eliminate the unused variable warning
// Configure I/O pins
pinMode(RUN, INPUT_PULLUP); // sets pin as input
pinMode(USER_OUTP, OUTPUT);
myservo.attach(PWM_OUTP); // attaches the servo on the D0 pin to the servo object
// Only supported on pins that have PWM
// Gen 4 devices including the M-SoM, P2, and Photon 2
analogWrite(PWM_OUTP, 255, 50);
Log.info("Starting RC Servo... ");
}
void loop()
{
static int old_pwm = -1; // servo position from previous reading
static int loop_cnt = 0;
if ( digitalRead(RUN) == LOW )
{
int pot = analogRead(SPD_POT); // read user input; rtns 0-4095
int pwm = map(pot, 0, 4095, 0, 180);// convert to 0-180 degree
if (pwm != old_pwm)
{
Log.info("Pot is %d/4095, pwm=%d ", pot, pwm);
old_pwm = pwm;
}
myservo.write(pwm);
//myservo.writeMicroseconds(map(pot, 0, 4095, 1000, 2000));
delay(100);
}
else
{
myservo.write(0);
//myservo.writeMicroseconds(1000);
delay(1000);
}
loop_cnt += 1;
digitalWriteFast(USER_OUTP, !pinReadFast(USER_OUTP)); // toggle LED
}