#include <Servo.h>
Servo servo; // create servo object to control a servo
#define PAUSE 5000 // duration of time servo will be at 180 degrees
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() {
pinMode(buttonPin, INPUT_PULLUP);
servo.attach(servoPin); // attach the servo instance to the servo PWM pin
servo.write(90); // rotate the servo to 90 degrees.
}
void loop() {
if (!digitalRead(buttonPin)) { // is the button pressed?
delay(150); // debounce the button ringing noise
for (int pos = 90; pos <= 180; pos++){
servo.write(pos);
delay(30); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, HIGH); // turn LED ON
delay(PAUSE); // pausing at 180 degrees
for (int pos = 180; pos >= 90; pos --){
servo.write(pos);
delay(30); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW); // turn LED OFF
}
}