#include <Servo.h>
const int joystickXPin = A0; // Analog pin for X-axis
const int joystickYPin = A1; // Analog pin for Y-axis
const int joystickButtonPin = 2; // Digital pin for the button
const int servoXPin = 9; // Digital pin for X-axis servo
const int servoYPin = 10; // Digital pin for Y-axis servo
Servo servoX;
Servo servoY;
void setup() {
// Start serial communication at 9600 baud rate
Serial.begin(9600);
// Set the joystick button pin as input
pinMode(joystickButtonPin, INPUT_PULLUP);
// Attach the servos to their respective pins
servoX.attach(servoXPin);
servoY.attach(servoYPin);
}
void loop() {
// Read the X and Y axis values
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
// Read the button state
int buttonState = digitalRead(joystickButtonPin);
// Map the analog values to servo angles (0 to 180)
int xServoAngle = map(xValue, 0, 1023, 0, 180);
int yServoAngle = map(yValue, 0, 1023, 0, 180);
// Move the servos to the mapped angles
servoX.write(xServoAngle);
servoY.write(yServoAngle);
// Print the values to the serial monitor
Serial.print("X: ");
Serial.print(xValue);
Serial.print(" | Y: ");
Serial.print(yValue);
Serial.print(" | X Servo: ");
Serial.print(xServoAngle);
Serial.print(" | Y Servo: ");
Serial.print(yServoAngle);
Serial.print(" | Button: ");
Serial.println(buttonState == LOW ? "Pressed" : "Released");
// Add a small delay to make the output more readable
delay(100);
}