
Sample code :
#include <Servo.h>
Servo myservo;
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 4; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int pos = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
myservo.attach(9);
}
void loop(){
// read the state of the pushbutton value:
if (digitalRead(buttonPin1) == LOW) {
digitalWrite(ledPin, LOW); //turn LED off
{ // in steps of 1 degree
myservo.write(0); // tell servo to go to position in variable ‘pos’
delay(15);
}
} else if (digitalRead(buttonPin2) == LOW) {
digitalWrite(ledPin, HIGH); //turn LED on
myservo.write(180); // tell servo to go to position in variable ‘pos’
delay(15);
}
}