arduino servo and delay
<p>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</p>

<pre><code>int x = 0;   

void pivot(){
for (pos = 0; pos &lt;= 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&lt;10; x++)
  {
    delay(1);
    ping();
  }
  x=0;
 }
for (pos = 180; pos &gt;= 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&lt;10; x++)
  {
    delay(1);
    ping();
  }
  x=0;
 }
}
</code></pre>