4

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);
    }
  }
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Herman
  • 41
  • 3

1 Answers1

2

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.

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39
Mode77
  • 991
  • 8
  • 28
  • Oh, so i set a minimum and maximum value in the attach? like this: myservo.attach(9, 1700,2400); I can't find exact values for the the different degrees. what does the numbers stand for? Sorry im a bit new to this :) – Herman Jun 22 '19 at 17:05
  • @Herman Yes; the actual pulse widths you pass in need to correspond with the minimum and maximum positions, which may or may not be (0° - 180°) for your particular servo. – Mode77 Jun 22 '19 at 17:10
  • @Herman Servos like the one you have operate from something called pulse-width modulation. Have a look at [this](https://en.wikipedia.org/wiki/Pulse-width_modulation). – Mode77 Jun 22 '19 at 17:18
  • Thanks, but this did not seem to fix the problem. The servo still tries to reach the "wrong" angles" even though i tried different values with no luck – Herman Jun 22 '19 at 17:19
  • @Herman It may still not work depending on the numbers you used. You need to know the exact numbers to use. You have to work out what signal pulse width gives you the min/max shaft positions. That information is typically in the datasheet. – Mode77 Jun 22 '19 at 17:23
  • Sadly i do not have the datasheet... It did not come with the servo. But thanks for the help! – Herman Jun 22 '19 at 17:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195408/discussion-between-delimiter-and-herman). – Mode77 Jun 22 '19 at 17:32