#include <Servo.h> // indicate that we want to use the Servo library
Servo servo; // initialize the Servo library
int servoPin = 9; // control lead of servo connected to pin 9
int angle = 0; // set the initial servo position in degrees
int LED = 10; // the pin for the LED
int BUTTON = 12; // the input pin where the pushbutton is connected
int val = 0; // val will be used to store the state of the input pin
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT_PULLUP); // and BUTTON is an input
servo.attach(servoPin); // indicate that servo motor is attached to the servoPin
}
void loop() {
val = digitalRead(BUTTON); // read input value and store it
// Check whether the input is HIGH (button pressed)
if (val != HIGH) {
digitalWrite(LED, HIGH); // turn LED on
delay(2000);
} else {
digitalWrite(LED, LOW); // turn LED off
for(angle = 0; angle < 180; angle++) // counts up from 0 to 180 (max angle) using the variable "angle"
{
servo.write(angle); // set the new angle
delay(15); // delay between the steps
}
for(angle = 180; angle > 0; angle--) // counts down from 0 to 180 (max angle) using the variable "angle"
{
servo.write(angle); // set the new angle
delay(15); // delay between the steps
}
}
}