#include <Servo.h>
#define VRX_PIN1 A0 // Arduino pin connected to VRX pin of first joystick
#define VRY_PIN1 A1 // Arduino pin connected to VRY pin of first joystick
#define VRX_PIN2 A2 // Arduino pin connected to VRX pin of second joystick
#define VRY_PIN2 A3 // Arduino pin connected to VRY pin of second joystick
#define VRX_PIN3 A4 // Arduino pin connected to VRX pin of third joystick
#define VRY_PIN3 A5 // Arduino pin connected to VRY pin of third joystick
#define SERVO_X_PIN1 2 // Arduino pin connected to Servo motor 1
#define SERVO_Y_PIN1 3 // Arduino pin connected to Servo motor 2
#define SERVO_X_PIN2 4 // Arduino pin connected to Servo motor 3
#define SERVO_Y_PIN2 5 // Arduino pin connected to Servo motor 4
#define SERVO_X_PIN3 6 // Arduino pin connected to Servo motor 5
#define SERVO_Y_PIN3 7 // Arduino pin connected to Servo motor 6
Servo xServo1; // create servo object to control servo 1
Servo yServo1; // create servo object to control servo 2
Servo xServo2; // create servo object to control servo 3
Servo yServo2; // create servo object to control servo 4
Servo xServo3; // create servo object to control servo 5
Servo yServo3; // create servo object to control servo 6
void setup() {
Serial.begin(9600);
xServo1.attach(SERVO_X_PIN1);
yServo1.attach(SERVO_Y_PIN1);
xServo2.attach(SERVO_X_PIN2);
yServo2.attach(SERVO_Y_PIN2);
xServo3.attach(SERVO_X_PIN3);
yServo3.attach(SERVO_Y_PIN3);
}
void loop() {
// read analog X and Y analog values from first joystick
int xValue1 = analogRead(VRX_PIN1);
int yValue1 = analogRead(VRY_PIN1);
// read analog X and Y analog values from second joystick
int xValue2 = analogRead(VRX_PIN2);
int yValue2 = analogRead(VRY_PIN2);
// read analog X and Y analog values from third joystick
int xValue3 = analogRead(VRX_PIN3);
int yValue3 = analogRead(VRY_PIN3);
int xAngle1 = map(xValue1, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int yAngle1 = map(yValue1, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int xAngle2 = map(xValue2, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int yAngle2 = map(yValue2, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int xAngle3 = map(xValue3, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int yAngle3 = map(yValue3, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
xServo1.write(xAngle1); // rotate servo motor 1
yServo1.write(yAngle1); // rotate servo motor 2
xServo2.write(xAngle2); // rotate servo motor 3
yServo2.write(yAngle2); // rotate servo motor 4
xServo3.write(xAngle3); // rotate servo motor 5
yServo3.write(yAngle3); // rotate servo motor 6
// print data to Serial Monitor on Arduino IDE
Serial.print("Joystick 1: ");
Serial.print(xValue1);
Serial.print(", ");
Serial.print(yValue1);
Serial.print(" => Servo Motor 1: ");
Serial.print(xAngle1);
Serial.print("°, ");
Serial.print(yAngle1);
Serial.print("°\t");
Serial.print("Joystick 2: ");
Serial.print(xValue2);
Serial.print(", ");
Serial.print(yValue2);
Serial.print(" => Servo Motor 2: ");
Serial.print(xAngle2);
Serial.print("°, ");
Serial.print(yAngle2);
Serial.print("°\t");
Serial.print("Joystick 3: ");
Serial.print(xValue3);
Serial.print(", ");
Serial.print(yValue3);
Serial.print(" => Servo Motor 3: ");
Serial.print(xAngle3);
Serial.print("°, ");
Serial.print(yAngle3);
Serial.println("°");
}