#include <Servo.h>
// Define the analog pins for the joystick
const int joystickXPin = A0;
const int joystickYPin = A1;
// Define the digital pin for the servo
const int servoPin9 = 9;
const int servoPin10 = 10;
// Create a Servo object
Servo myServo;
Servo myServo2;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Attach the servo to the servo pin
myServo.attach(servoPin9);
myServo2.attach(servoPin10);
}
void loop() {
// Read the joystick values
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
// Map the joystick values to servo angle (0 to 180 degrees)
int servoXAngle = map(xValue, 0, 1023, 0, 180);
int servoYAngle = map(yValue, 0, 1023, 0, 180);
// Write the angle to the servo
myServo.write(servoXAngle);
myServo2.write(servoYAngle);
// Print the servo angle to the Serial Monitor
Serial.print("Joystick X Value: ");
Serial.print(xValue);
Serial.print(" -> Servo X Angle: ");
Serial.println(servoXAngle);
Serial.print("Joystick Y Value: ");
Serial.print(yValue);
Serial.print(" -> Servo Y Angle: ");
Serial.println(servoYAngle);
// Delay for a short period of time before reading again
delay(100);
}