// Pin setup for joystick and LEDs
const int VRx = A2; // Joystick X axis
const int VRy = A1; // Joystick Y axis
const int ledRed = 8; // Left movement
const int ledGreen = 9; // Right movement
const int ledBlue = 10; // Up movement
const int ledYellow = 11; // Down movement
int xValue = 0; // X axis value
int yValue = 0; // Y axis value
void setup() {
// Set LED pins as outputs
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledYellow, OUTPUT);
// Initialize serial communication for debugging (optional)
Serial.begin(9600);
}
void loop() {
// Read the joystick's X and Y axis values
xValue = analogRead(VRx);
yValue = analogRead(VRy);
// Print joystick values for debugging (optional)
Serial.print("X-axis: ");
Serial.print(xValue);
Serial.print(" | Y-axis: ");
Serial.println(yValue);
// Check the X axis for left or right movement
if (xValue < 400) {
digitalWrite(ledGreen, HIGH); // Left
} else {
digitalWrite(ledGreen, LOW);
}
if (xValue > 600) {
digitalWrite(ledRed, HIGH); // Right
} else {
digitalWrite(ledRed, LOW);
}
// Correct Y axis logic for upward and downward movement
if (yValue < 400) {
digitalWrite(ledYellow, HIGH); // Down (bottom)
} else {
digitalWrite(ledYellow, LOW);
}
if (yValue > 600) {
digitalWrite(ledBlue, HIGH); // Up (top)
} else {
digitalWrite(ledBlue, LOW);
}
delay(100); // Small delay to avoid flickering
}