#include <Servo.h>
Servo servoX; // create servo object to control a servo
Servo servoY; // create servo object to control a servo
const int joystickXPin = A0; // Joystick X axis
const int joystickYPin = A1; // Joystick Y axis
const int switchPin = 7; // Joystick select switch
const int ledPin = 13; // LED pin
const int servoXPin = 3; // Servo connected to digital pin 3
const int servoYPin = 5; // Servo connected to digital pin 5
void setup() {
servoX.attach(servoXPin); // attaches the servo on pin 3 to the servo object
servoY.attach(servoYPin); // attaches the servo on pin 5 to the servo object
pinMode(switchPin, INPUT_PULLUP); // initialize switch pin as input with pull-up resistor
pinMode(ledPin, OUTPUT); // initialize LED pin as output
Serial.begin(9600); // initialize serial communication for debugging
}
void loop() {
int xValue = analogRead(joystickXPin); // read the X axis value
int yValue = analogRead(joystickYPin); // read the Y axis value
// map the analog values to servo angle (0-180)
int xServoPos = map(xValue, 0, 1023, 0, 180);
int yServoPos = map(yValue, 0, 1023, 0, 180);
// set the servo positions
servoX.write(xServoPos);
servoY.write(yServoPos);
// read the switch state
int switchState = digitalRead(switchPin);
// if the switch is pressed, turn on the LED
if (switchState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// print values to the serial monitor for debugging
Serial.print("X: ");
Serial.print(xServoPos);
Serial.print(" | Y: ");
Serial.print(yServoPos);
Serial.print(" | Switch: ");
Serial.println(switchState);
delay(20); // small delay for stability
}