#include <Servo.h>
Servo servo1; // Create a Servo object for the first servo
Servo servo2;
Servo servo3; // Create a Servo object for the first servo
Servo servo4; // Create a Servo object for the second servo
const int joystickHorzPin = A0; // Analog pin connected to the joystick HORZ
const int joystickVertPin = A1;
void setup() {
servo1.attach(9); // Pin connected to the first servo control wire
servo2.attach(10); // Pin connected to the second servo control wire
servo3.attach(8); // Pin connected to the first servo control wire
servo4.attach(12);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int joystickValue = analogRead(joystickHorzPin);
int joystickValue1 = analogRead(joystickVertPin); // Read the value from the joystick HORZ pin
// Map the joystick value (0-1023) to a range for the servos (0 to 180 degrees)
int servoAngle = map(joystickValue, 0, 1023, 0, 180);
servo1.write(servoAngle); // Set the position of the first servo
servo2.write(servoAngle); // Set the position of the second servo
int servoAngle1 = map(joystickValue1, 0, 1023, 0, 180);
servo3.write(servoAngle1); // Set the position of the first servo
servo4.write(servoAngle1);
// Print joystick value and servo angle for debugging
Serial.print("Joystick Value: ");
Serial.print(joystickValue);
Serial.print(" | Servo Angle: ");
Serial.println(servoAngle);
delay(15); // Wait for the servos to reach the position
}