#include <Servo.h>
Servo myServo; // create servo object to control a servo
const int switchPin = 2; // the number of the switch pin
const int servoPin = 11; // the number of the servo pin
const int ledPin = 13; // the number of the LED pin
int switchState = 0; // variable for reading the switch status
void setup() {
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// initialize the switch pin as an input
pinMode(switchPin, INPUT);
// attach the servo object to the servo pin
myServo.attach(servoPin);
}
void loop(){
// get the state of the switch
switchState = digitalRead(switchPin);
if (switchState == HIGH) { // if the switch is on
// turn on the LED
digitalWrite(ledPin, HIGH);
// wait half a second
delay(300);
// push the servo arm out
myServo.write(0);
} else { // if the switch is off
// turn off the LED
digitalWrite(ledPin, LOW);
// pull the servo arm in
myServo.write(180);
}
}
Comments