0

I have a servo that will sweep from 0 to 180 degrees then back to 0 however i cannot use the delay() function as i need the sweep to be interrupted by a motion sensor. I have written some code however the sweep is not smooth, the servo jumps around sometimes. What have i not done correctly? Thanks in advance

int x = 0;   

void pivot(){
for (pos = 0; pos <= 180; pos += 5) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo_2.write(pos);              // tell servo to go to position in variable 'pos'
//delay(15); 
  for (x; x<10; x++)
  {
    delay(1);
    ping();
  }
  x=0;
 }
for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees
myservo_2.write(pos);              // tell servo to go to position in variable 'pos'
//delay(15);
for (x; x<10; x++)
  {
    delay(1);
    ping();
  }
  x=0;
 }
}
LEMONVirus99
  • 1
  • 1
  • 2

1 Answers1

0

I think the problem is simply that you are trying to sweep too fast.

180 degrees in increments of 5 is only 36 steps, so that's 72 there and back. With a 1 ms delay looped 10 times, you are trying to move the servo through its full range and back in 0.72 seconds. This is just a little faster than a typical RC servo can move. 0.24 seconds for 60 degrees is typical for a basic servo, so if your full range is about 120 it would take it roughly 1 second. That said, for all I know you may be using a fast servo with a 60 degree range.

The other possibility is that when you say not smooth you don't mean that it's jumping around randomly, but just that it's jittering. I would expect with 5 degree jumps every 10 ms you would get a little bit of jumping, 10 Hz is close to the frequency of a Harley idling and 5 degrees is too large of a movement to appear smooth at that speed.

Do you need the servo to stop during the 10 pings, or can it sweep continuously? If it can then I would decrease the number of pings per stop and proportionally decrease the step size. IE step 1 degree and ping 2 times.

Paul S
  • 3
  • 2