Skip to content

Servo: Calling write() before attach() causes problems #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrisfraser opened this issue Sep 29, 2015 · 1 comment
Closed

Servo: Calling write() before attach() causes problems #839

chrisfraser opened this issue Sep 29, 2015 · 1 comment

Comments

@chrisfraser
Copy link
Contributor

In other servo libraries calling write() or writeMicroseconds() before attach sets the initial position of the servo on attach.

This causes the servo to not work in the latest staging release. See code to reproduce below.

#include <Servo.h>

Servo servo;

int servoPin = 13;

void setup()
{
  servo.writeMicroseconds(544);

  servo.attach(servoPin);

  delay(3000);
}

void loop()
{
  int us;

  for (us = 544; us <= 2400; us += 2)
  {
    servo.writeMicroseconds(us);
    delay(3);
  }
  for (us = 2400; us >= 544; us -= 2)
  {
    servo.write(us);
    delay(3);
  }
}
@chrisfraser
Copy link
Contributor Author

Upon further investigation, the _minUs and _maxUs variables are only initialised in attach(). This means that the values are 0 when calling write().

In write() the value will evaluate to 0.

value = map(value, 0, 180, _minUs, _maxUs);

In writeMicroseconds() the value will evaluate to 0 too.

value = max(_minUs, min(_maxUs, value));

Will create a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant