Loading...
Pages: [1]   Go Down
Author Topic: Servo Jerk  (Read 337 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The project we're currently doing for class is requiring us to use a few servo motors to create an animatronics bat.  However I am having coding problems with the servo's. They simply work fine, it will go from one degree to the next, but when it resets to it's original position and goes the other way it seems to jerk real fast and does not look smooth at all.

Any help on how to fix this would be helpful!

Here is my code, it is similar to the one on the arduino reference website, I've just added in a few extra lines.
Code:

#include <Servo.h>
 
Servo myservoA;
Servo myservoB;

int pos = 0;    // variable to store the servo position
 
void setup()
{
  myservoA.attach(9); 
  myservoB.attach(8);
}
 
 
void loop()
{
  for(pos = 50; pos < 1; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservoA.write(pos);              // tell servo to go to position in variable 'pos'
    (15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 140; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservoA.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  {
  for(pos = 90; pos < 50; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservoB.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 0; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservoB.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);   
  }   
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 312
Posts: 35483
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you need a few more useless curly braces in the code.

Or, more likely, you need to tell us how the servos are powered and what load is being applied to the servos.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(pos = 50; pos < 1; pos += 1)  // goes from 0 degrees to 180 degrees
Why bother with comments?
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

UK
Offline Offline
Tesla Member
***
Karma: 89
Posts: 6368
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The start position for each loop is not the same as the end position from the previous loop, so the servo jumps to the start position as fast as it can. If you want a smooth continuous motion, you need to make sure that you don't make any abrupt changes to the servo position. (If the code actually did what the comments implied, you wouldn't have this problem.)
Logged

Pages: [1]   Go Up
Print
 
Jump to: