#define LED_PORT PORTA // Port where LEDs are connected
#define LED_DDR DDRA // Data direction register for LED port
#define JOY_Y A0 // Joystick Y-axis connected to A0
#define JOY_X A2 // Joystick X-axis connected to A2
void setup() {
LED_DDR = 0xFF; // Set PORTA (A22-A29) as output (all LEDs)
}
void loop() {
int adcX = analogRead(JOY_X); // Read X-axis of the joystick
int adcY = analogRead(JOY_Y); // Read Y-axis of the joystick
uint8_t ledPattern = 0; // Default LED pattern (all LEDs OFF)
// Joystick moved up or down, turn on all LEDs
if (adcY < 300 || adcY > 700) {
ledPattern = 0xFF;
}
// Joystick moved left, turn on 9th LED (A22)
else if (adcX < 700) {
ledPattern = 0b10000000;
}
// Joystick moved right, turn on 1st LED (A29)
else if (adcX > 300) {
ledPattern = 0b00000001;
}
// If joystick is neutral or in other direction, turn off 1st and 9th LEDs
if (adcX >= 300 && adcX <= 700 && adcY >= 300 && adcY <= 700) {
ledPattern &= ~(0b10000000); // Turn off 9th LED (A22)
ledPattern &= ~(0b00000001); // Turn off 1st LED (A29)
}
LED_PORT = ledPattern; // Output the LED pattern to the LEDs
}