#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int buttonPin = 12; // declare a button pin
int servoPin = 13; // declare the servo pin
int ledPin = 11; // add an LED on pin 11
void setup() {
myservo.attach(13); // attaches the servo on pin 13 to the servo object
pinMode(buttonPin, INPUT_PULLUP); // configure pin with pullup resistor.
// Pressed = LOW, normal = HIGH
}
void loop() {
if (digitalRead(buttonPin) == 0) { // read the button pin to see if it is LOW
delay(150); // debounce
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(ledPin, HIGH); // turn LED ON
delay(5000); // servo wait for a moment at 180 degrees
for (pos = 180; pos >= 0; pos -= 1) { // returns from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(ledPin, LOW); // turn LED OFF
}
}
}
}