Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int LED_PINS[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
- const int JOYSTICK_X = A0;
- const int JOYSTICK_Y = A1;
- int currentPosition = 4; // Start in the center
- void setup() {
- for (int i = 0; i < 9; i++) {
- pinMode(LED_PINS[i], OUTPUT);
- }
- pinMode(JOYSTICK_X, INPUT);
- pinMode(JOYSTICK_Y, INPUT);
- }
- void loop() {
- int xValue = analogRead(JOYSTICK_X);
- int yValue = analogRead(JOYSTICK_Y);
- // Turn off all LEDs
- for (int i = 0; i < 9; i++) {
- digitalWrite(LED_PINS[i], LOW);
- }
- // Update position based on joystick input
- if (xValue < 300 && currentPosition % 3 != 0) currentPosition--;
- if (xValue > 700 && currentPosition % 3 != 2) currentPosition++;
- if (yValue < 300 && currentPosition > 2) currentPosition -= 3;
- if (yValue > 700 && currentPosition < 6) currentPosition += 3;
- // Turn on LED at current position
- digitalWrite(LED_PINS[currentPosition], HIGH);
- delay(100); // Small delay for stability
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement