Go Down

Topic: servo is jerky (Read 511 times) previous topic - next topic

freaky-m0

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:
Code: [Select]
#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:
Code: [Select]
#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!

JimboZA

Is the hardware the same in both cases?
meArm build blog:     http://jimbozamearm.blogspot.com/

Please don't PM for technical advice. Firstly my advice is sometimes wrong, and second, in the forum you get a broader, more timely, range of responses.

freaky-m0

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

JimboZA

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.
meArm build blog:     http://jimbozamearm.blogspot.com/

Please don't PM for technical advice. Firstly my advice is sometimes wrong, and second, in the forum you get a broader, more timely, range of responses.

freaky-m0

#4
Feb 23, 2013, 12:19 pm Last Edit: Feb 23, 2013, 12:24 pm by freaky-m0 Reason: 1
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 :(

Code: [Select]
#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;
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy