#include <Servo.h>
#define BUTTON_PIN 3
#define RED_LED_PIN 2
#define GREEN_LED_PIN 10
#define SWITCH_PIN 12
#define DIAL_PIN A0
#define RIGHTSERVO_PIN 9
#define LEFTSERVO_PIN 6
Servo leftservo, rightservo; // create servo object to control a servo
int pos = 90;
int dialval = 0;
void setup() {
leftservo.attach(LEFTSERVO_PIN); // attaches the servo on pin 9 to the servo object
rightservo.attach(RIGHTSERVO_PIN); // attaches the servo on pin 9 to the servo object
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(SWITCH_PIN, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
}
void loop() {
if ( digitalRead(SWITCH_PIN) == HIGH ) { // switch is on the right
//the dial controls the servo positions
//change the LED's
digitalWrite(RED_LED_PIN,LOW);
digitalWrite(GREEN_LED_PIN,HIGH);
dialval = analogRead(DIAL_PIN); // reads the value of the potentiometer (value between 0 and 1023)
pos = map(dialval, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
leftservo.write(pos); // tell servos to go to their positions
rightservo.write(180-pos); //the servo is rotated 180 deg, so notice: 180 - rightpos
delay(10);
} else { // the switch is on the left
//change the LED's
digitalWrite(RED_LED_PIN,HIGH);
digitalWrite(GREEN_LED_PIN,LOW);
if ( digitalRead(BUTTON_PIN) == LOW ) {
//the green button centers the servos
leftservo.write(90);
rightservo.write(90);
}
}
}