Hi I recently got an arduino and am trying to make a servo sweep back and forth without using the delay funtion. When I load my code, the servo sweeps back and forth several times after which it freaks out and starts flipping back and forth a small amount very quickly. I've tried rewriting the code a couple different ways to no avail. Here's my code, any help would be appreciated!
#include <Servo.h>
Servo myservo;
int pos = 0;
int prevmillis = 0;
int interval = 15;
boolean growing = true;
void setup()
{
myservo.attach(9);
}
void loop()
{
if ( millis() - prevmillis > interval ) {
myservo.write(pos);
if ( growing ) {
pos += 1;
} else {
pos -= 1;
}
if ( pos > 179 )
growing = false;
if ( pos < 1 )
growing = true;
prevmillis = millis();
}
}