#include <Servo.h>
#define RED_BUTTON_PIN 3
#define BLUE_BUTTON_PIN A1
#define WHITE_BUTTON_PIN A4
#define BLACK_BUTTON_PIN A2
#define GREEN_BUTTON_PIN A3
#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 leftpos = 90;
int rightpos = 90;
int dialval = 0;
int horz = 512;
int vert = 512;
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(RED_BUTTON_PIN, INPUT_PULLUP);
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP);
pinMode(WHITE_BUTTON_PIN, INPUT_PULLUP);
pinMode(BLACK_BUTTON_PIN, INPUT_PULLUP);
pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP);
pinMode(SWITCH_PIN, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
leftservo.write(90); // tell servo to go to position in variable 'pos'
rightservo.write(90);
}
void loop() {
// put your main code here, to run repeatedly:
if ( digitalRead(SWITCH_PIN) == HIGH ) { //the dial controls the servo positions
//the dial controls the servo positions
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)
leftpos = pos; // set the servo positions, and write them later
rightpos = 180-pos; // set the servo positions, and write them later
} else {
//the joystick controls the servo positions
digitalWrite(RED_LED_PIN,HIGH);
digitalWrite(GREEN_LED_PIN,LOW);
if ( digitalRead(RED_BUTTON_PIN) == LOW ) {
//the green button centers the servos
leftpos = 90;
rightpos = 90;
}
// process the BUTTON values to modify the servo positions
if ( digitalRead(BLUE_BUTTON_PIN) == LOW ) { //
leftpos ++ ;
rightpos ++ ;
}
if ( digitalRead(GREEN_BUTTON_PIN) == LOW ) { // I used 500 instead of 512 to allow for a small error deadband
leftpos -- ;
rightpos -- ;
}
if ( digitalRead(WHITE_BUTTON_PIN) == LOW ) { // the horizontal joystick moves the servos in opposite directions
leftpos -- ;
rightpos ++ ;
}
if ( digitalRead(BLACK_BUTTON_PIN) == LOW ) {
leftpos ++ ;
rightpos -- ;
}
}
//prevent limit violations on both servos
leftpos = constrain(leftpos,0,180);
rightpos = constrain(rightpos,0,180);
leftservo.write(leftpos); // tell servos to go to their positions
rightservo.write(180-rightpos); //the servo is rotated 180 deg, so notice: 180 - rightpos
delay(10);
}