user37778: I'm trying to get my servo motor to sweep from 0 to 180 degrees then back to 0 degrees for infinite amount of times. I using the following code but my motor keeps resetting when it get backs to 0 degrees (meaning it goes 0-->180-->0 then completes a 360 and starts over). Also the 0-180 degree sweep appears to be more of a 0-270 degree sweep. Anyone running into these problems?
int angle = 0;

void setup() {
  myservo.attach(9);
  myservo.write(angle);
}

void loop()
{
  for (angle = 0; angle < 180; angle += 1)
  {
    myservo.write(angle);
    delay (50);
  }

  for (angle = 180; angle > 0; angle -= 1)
  {
    myservo.write(angle);    
    delay(50);
  }   
}


user27653: Are you trying to have the servo sweep at a particular speed? Why not just use:
void loop()
{
  angle=180;
  myservo.write(angle);

  delay(500); //Allow 0.5 seconds for servo to complete it's move; adjust as necessary depending on servo's rotation speed.

  angle=0;
  myservo.write(angle);

  delay(500); //Allow 0.5 seconds for servo to complete it's move; adjust as necessary depending on servo's rotation speed.
}


user20513: I had a bunch of SG90 mini servos so I tried your code. My servos turn 0 -> 180 -> 0 -> 180 -> 0 -> 180, over and over. No 360 spins at all.

Maybe your servo is defect? (can you check with another servo?)
Maybe it is not getting enough power? (how are you powering it?) It needs at least 4.8V and at least 220mA
Maybe it doesn't get a strong clean PWM signal? (You'll probably need an oscilloscope to check this)


user20570: Is your servo the modified 360 degree type with no position feedback (meaning the internal feedback potentiometer has been disabled)?

37778: <p>I'm trying to get my servo motor to sweep from 0 to 180 degrees then back to 0 degrees for infinite amount of times. I using the following code but my motor keeps resetting when it get backs to 0 degrees (meaning it goes 0-->180-->0 then completes a 360 and starts over). Also the 0-180 degree sweep appears to be more of a 0-270 degree sweep. Anyone running into these problems?</p>

<pre class="lang-cpp prettyprint-override"><code>int angle = 0;

void setup() {
  myservo.attach(9);
  myservo.write(angle);
}

void loop()
{
  for (angle = 0; angle &lt; 180; angle += 1)
  {
    myservo.write(angle);
    delay (50);
  }

  for (angle = 180; angle &gt; 0; angle -= 1)
  {
    myservo.write(angle);    
    delay(50);
  }   
}
</code></pre>
 27653: <p>Are you trying to have the servo sweep at a particular speed? Why not just use:</p>

<pre><code>void loop()
{
  angle=180;
  myservo.write(angle);

  delay(500); //Allow 0.5 seconds for servo to complete it's move; adjust as necessary depending on servo's rotation speed.

  angle=0;
  myservo.write(angle);

  delay(500); //Allow 0.5 seconds for servo to complete it's move; adjust as necessary depending on servo's rotation speed.
}
</code></pre>
 20513: <p>I had a bunch of SG90 mini servos so I tried your code. My servos turn 0 -> 180 -> 0 -> 180 -> 0 -> 180, over and over. No 360 spins at all.</p>

<ul>
<li>Maybe your servo is defect? (can you check with another servo?)</li>
<li>Maybe it is not getting enough power? (how are you powering it?) It needs at least 4.8V and at least 220mA</li>
<li>Maybe it doesn't get a strong clean PWM signal? (You'll probably need an oscilloscope to check this)</li>
</ul>
 20570: <p>Is your servo the modified 360 degree type with no position feedback (meaning the internal feedback potentiometer has been disabled)?</p>
