arduino-libraries / Servo

Servo Library for Arduino
http://arduino.cc/
GNU Lesser General Public License v2.1
245 stars 255 forks source link

write() on Arduino Uno Rev4 Wifi only works with angle #133

Open thomashargrove opened 3 days ago

thomashargrove commented 3 days ago

The Servo.h header file has this comment for write():

// if value is < 200 it's treated as an angle, otherwise as pulse width in microseconds

This is true for AVR boards:

void Servo::write(int value)
{
  if(value < MIN_PULSE_WIDTH)
  {  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
    if(value < 0) value = 0;
    if(value > 180) value = 180;
    value = map(value, 0, 180, SERVO_MIN(),  SERVO_MAX());
  }
  this->writeMicroseconds(value);
}

But the Uno Rev4 implementation seems to only work with angles:

void Servo::write(int angle)
{
    if (servoIndex != SERVO_INVALID_INDEX) {
        ra_servo_t *servo = &ra_servos[servoIndex];
        angle = constrain(angle, 0, 180);
        writeMicroseconds(map(angle, 0, 180, servo->period_min, servo->period_max));
    }
}

From a quick scan of the different implementations:

  1. Avr, mbed, megaavr, sam, samd, xmc - Supports angles and microseconds
  2. nrf52, renesas, stm32f4 - Only works with angles

Should be an easy fix, happy to send a PR if someone will approve it.