#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int LED = 13; // the pin for the LED
const int BUTTON = 7; // the input pin where the pushbutton is connected
int valb = 0; //val will be used to store the state of the input pin
int old_val = 0; // stores the previous value of valb
int state = 0; // 0 = LED off and 1 = LED on
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(LED, OUTPUT); // tell Arduino LED is and output
pinMode(BUTTON, INPUT); // and BUTTON is and input
}
void loop() {
valb = digitalRead(BUTTON); // read input value and store it
// check whether the iput is HIGH (button pressed)
if ((valb == HIGH) && (old_val == LOW)){
state = 1 - state;
delay(10);
}
if ((valb == LOW) && (old_val == HIGH)) {
delay(10);
}
old_val = valb;
if (state == 1) {
digitalWrite(LED,HIGH);
val = 0;
myservo.write(val); // Write value to the servo
} else {
digitalWrite(LED, LOW); //turn LED OFF
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
}