Go Down

Topic: [SOLVED] Servomotor jerks (Read 156 times) previous topic - next topic

iacoposk8

Jul 11, 2018, 08:29 pm Last Edit: Jul 13, 2018, 07:58 pm by iacoposk8
Hello everyone! I'm testing with servomotors (mg996r).
I used this code but when I run it the servomotor jumps for a minute (see the this video: https://drive.google.com/open?id=1y83XvZb10X7m79iMCFHbqaGK0Rr9gdnH) and then starts working correctly, how do I solve it?
Code: [Select]

#include <Servo.h>
 
Servo myservo;

int servo_speed = 15;
int moveTO(Servo servo, int degree){
  int pos = servo.read();
  if(pos == degree){
    return pos;
  }
  if(pos < degree){
    for(; pos < degree; pos += 1)
    {
      servo.write(pos);
      delay(servo_speed);
    }
  } else {
    for(; pos >= degree; pos -= 1)
    {
      myservo.write(pos);
      delay(servo_speed);
    }
  }
  return pos;
}
 
void setup()
{
  myservo.attach(9);
  Serial.begin(9600);
}
 
void loop()
{
  moveTO(myservo, 76);
  delay(1000);
  moveTO(myservo, 152);
  delay(1000);
  moveTO(myservo, 0);
  delay(1000);
}

FredScuttle

When you attach a servo it goes to 90 degrees by default, if you want a different start position, say 15, you can specify it before attaching, do this in setup():
Code: [Select]
servo.write(15);
servo.attach(9);

groundFungus

#2
Jul 13, 2018, 03:09 pm Last Edit: Jul 13, 2018, 03:09 pm by groundFungus
Code: [Select]
int pos = servo.read();
Do you know that the servo.read() function does not return the actual position of the servo, but the last position that was sent to the servo with servo.write()?

iacoposk8

Perfect! thanks, works with 93!

Go Up