#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 val; // variable to read the value from the analog pin
void setup() {
rightservo.attach(RIGHTSERVO_PIN);
leftservo.attach(LEFTSERVO_PIN);
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) {
digitalWrite(RED_LED_PIN,LOW);
digitalWrite(GREEN_LED_PIN,HIGH);
val = analogRead(DIAL_PIN); // 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)
rightservo.write(180 - val); // sets the servo position according to the scaled value
leftservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
if (digitalRead(SWITCH_PIN) == LOW) {
digitalWrite(RED_LED_PIN,HIGH);
digitalWrite(GREEN_LED_PIN,LOW);
if (digitalRead(BUTTON_PIN) == LOW) {
rightservo.write(90); // sets the servo position according to the scaled value
leftservo.write(90); // sets the servo position according to the scaled value
delay(15);
} // waits for the servo to get there
}
}