Servo motor keeps trying to reach beneath 134°, what is wrong?

Viewed 280

I am trying to get the Arduino Uno board to control a gripper driven by a servo motor. The servo tries to go beneath 134° which is mechanically impossible. What can I do to fix this?

I tried to limit the motor to 180°, when it was not a reading of wanted buttons it would keep main position (closed, 180)

#include <Servo.h>

Servo myservo;

char reading;
int pos;

void setup() {
  // put your setup code here, to run once:
  myservo.attach(9);
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {

    reading = Serial.read();
    Serial.print(reading);
    if (reading == 'W' || reading == 'w') {
      pos = 134;
      myservo.write(pos);
      Serial.println("Open");
    }
    else if (reading == 'C' || reading == 'c') {
      pos = 180;
      myservo.write(pos);
      Serial.println("Close");
    }
    else if (reading != 'W' || reading != 'C') {
      myservo.write(180);
    }
  }
}
1 Answers

In your code, you only call attach() passing in the pin number.

The servo library requires you to specify the minimum and maximal position of the servo shaft when calling attach(). If you don't, you could end up in a situation where you're calling valid positions, but the servo either won't go far enough or tries to go further than mechanically possible.

The library needs to the know the minimum and maximum signal pulse widths so that when you call Servo.write() passing in the shaft angle, it can work out the corresponding duty cycle.

Related