#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
#define BUTTON_PIN 2 // Push button connected to digital pin 2
void setup() {
LED_DDR = 0xFF; // Set PORTA (A22-A29) as output (all LEDs)
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
}
void loop() {
int adcX = analogRead(JOY_X); // Read X-axis of the joystick
int adcY = analogRead(JOY_Y); // Read Y-axis of the joystick
bool buttonState = digitalRead(BUTTON_PIN) == LOW; // Button is active LOW
uint8_t ledPattern = 0; // Default LED pattern (all LEDs OFF)
if (buttonState) {
// Read Y-axis if button is pressed
if (adcY < 300) {
ledPattern = 0b00001111; // Lower half LEDs ON
} else if (adcY > 700) {
ledPattern = 0b11110000; // Upper half LEDs ON
}
} else {
// Read X-axis if button is not pressed
if (adcX < 300) {
ledPattern = 0b10000000; // Leftmost LED ON
} else if (adcX > 700) {
ledPattern = 0b00000001; // Rightmost LED ON
}
}
LED_PORT = ledPattern; // Output the LED pattern to the LEDs
}