#include <Servo.h>
const int servo1Pin = 9;
const int servo2Pin = 10;
const int joystickX = A0;
const int joystickY = A1;
const int button = 2;
const int joystickhomeX = 512;
const int joystickhomeY = 512;
const int joystickdeadband = 50;
const int servo1left = 0;
const int servo1right = 180;
const int servo2up = 0;
const int servo2down = 0;
const int servo1home = 90;
const int servo2home = 90;
const int servo1step = 1;
const int servo2step = 1;
Servo servo1;
Servo servo2;
int servo1Position = servo1home;
int servo2Position = servo2home;
void setup() {
servo1.attach(servo1);
servo2.attach(servo2);
pinMode(button, INPUT_PULLUP);
}
void loop() {
// read the joystick x and y values
int joystickValueX = analogRead(joystickX);
int joystickValueY = analogRead(joystickY);
// check if the joystick is in the left position
if(joystickValueX < joystickValueY - joystickdeadband) {
if (servo1Position > servo1left){
servo1Position -= servo1step;
}
}
// check if the joystick is in the right position
if(joystickValueX > joystickValueY + joystickdeadband) {
if (servo1Position < servo1right){
servo1Position += servo1step;
}
}
// check if the joystick is in the up position
if(joystickValueX < joystickValueY - joystickdeadband) {
if (servo2Position > servo2up){
servo2Position -= servo2step;
}
}
// check if the joystick is in the down position
if(joystickvalueX > joystickValueY + joystickdeadband) {
if (servo2Position < servo2down0 {
servo2Position += servo2step;
}
}
// check if the button is pressed and move there home position
if (digitalRead(button) == LOW){
servo1Position = servo1home;
servo2Position = servo2home;
}
servo1.write(servo1Position);
servo2.write(servo2Position);
delay(10);
}