#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position 0 to 180 degrees
int delta = 1;
void setup() {
pinMode(10, INPUT_PULLUP);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(pos);
delay(200);
}
void loop() {
if (digitalRead(10) == LOW) {
delta = 0;
}
pos = pos + delta;
if (pos >= 180) {
delta = -1;
} else if (pos <= 0) {
delta = +1;
}
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}