// ---------------------------------------------------------- // Servo and LED w/ switch // // Setup: > Wire a switch between digital pin 5 and ground // > Wire an LED from Digital Pin 6 thru 330 ohm resistor to gnd // > Connect a servo to +, gnd and digital pin 9 // ----------------------------------------------------------- int pos = 0; // variable to store the servo position int swPin = 5; // variable for pin number of switch boolean swState; // variable for recording "state" of the switch int servoPin = 9; // variable for servo pin number int ledPin = 6; // create variable for LED pin number #include Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created void setup() { pinMode(ledPin, OUTPUT); // initialize the digital pin output (for LED.) pinMode(swPin, INPUT_PULLUP); // Sets digital pin 5 for input pinMode(servoPin, OUTPUT); myservo.attach(servoPin); // attaches the servo on pin 6 to the servo object } void loop() { swState = digitalRead(swPin); if(swState == LOW){ // (LOW means button pressed.) digitalWrite(ledPin, HIGH); // turn LED on: myservo.write(165); // move servo to position (180 max) } else { digitalWrite(ledPin, LOW); // turn LED off: myservo.write(10); // move servo to position (0 minimum) } }