servo is jerky

Hi! I coded a little thing for a servo engine just for testing. But i recognize that the servo is always jerky with my code. If i use it with another code (partially from a example) it runs smooth. Maybe someone can see my fault? I added the "Serial.println(b);" & "Serial.println(a);" line to see if the for loop is running correct and yes in both codes it runs the same way.

jerky code:

#include <Servo.h>

Servo myservo; // create Servo Object "servo"

int pos = 0;
int pos_old = pos;
int i;
char buffer[4]; //max. Zahl "9999"


void setup()
{
  myservo.attach(9); // attaches servo to pin 9
  Serial.begin(9600); // Baudrate 9600
}

void loop()
{
  Serial.print("Servo Position 0-179: ");
  while(pos == pos_old) 
  {
    if (Serial.available() > 0) 
    {
      Serial.flush();
      delay(5);
      i=0;
      while(i<5)
      {  
        buffer[i] = Serial.read();
        i++;
      }
      Serial.flush();
      pos = atoi(buffer);
      Serial.print("Bewege um: ");
      Serial.println(pos);
    }
  }
  for(int a = pos_old; pos >a; a+=1)
  {
    myservo.write(a);
    delay(15);
    Serial.println(a);
  }
  for(int b = pos_old; pos <b; b-=1)
  {
    myservo.write(b);
    delay(15);
    Serial.println(b);
  }
  Serial.println("Postion erreicht");
  pos_old = pos;
}

good code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);
    Serial.println(pos);    // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);
    Serial.println(pos);    // waits 15ms for the servo to reach the position 
  } 
}

Thanks for help!

Is the hardware the same in both cases?

Yes exactly the same hardware. I only change the software.

Perhaps try the code with the problem, but with various lines //'d out (so that it still compiles of course 8) ) and see if the problem goes away.

I changed the code like you said and figured out that my readout/conversion of the position number from serial might be the problem. The code below works! Any ideas? As you can see i print the integer variable to the serial port and i can see a correct integer number.. so i dont understand whats the problem :frowning:

#include <Servo.h>

Servo myservo; // create Servo Object "servo"

int pos = 0;
int pos_old = pos;
int i;
char buffer[4]; //max. Zahl "9999"


void setup()
{
  myservo.attach(9); // attaches servo to pin 9
  Serial.begin(9600); // Baudrate 9600
}

void loop()
{
  Serial.print("Servo Position 0-179: ");
  while(pos == pos_old) 
  {
    //if (Serial.available() > 0) 
    //{
      //Serial.flush();
      //delay(5);
      //i=0;
      //while(i<5)
      //{  
        //buffer[i] = Serial.read();
        //i++;
      //}
      //Serial.flush();
      //pos = atoi(buffer);
      //Serial.print("Bewege um: ");
      //Serial.println(pos);
    //}
    pos = random(0,179);
  }
  for(int a = pos_old; pos >a; a+=1)
  {
    myservo.write(a);
    delay(15);
    Serial.println(a);
  }
  for(int b = pos_old; pos <b; b-=1)
  {
    myservo.write(b);
    delay(15);
    Serial.println(b);
  }
  Serial.println("Postion erreicht");
  pos_old = pos;
}