Advertisement
belrey10

37 Sensor x Cogsworth City DLC 01 – Lesson 1: The Joystick Navigator

Nov 4th, 2024 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int LED_PINS[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
  2. const int JOYSTICK_X = A0;
  3. const int JOYSTICK_Y = A1;
  4.  
  5. int currentPosition = 4; // Start in the center
  6.  
  7. void setup() {
  8.   for (int i = 0; i < 9; i++) {
  9.     pinMode(LED_PINS[i], OUTPUT);
  10.   }
  11.   pinMode(JOYSTICK_X, INPUT);
  12.   pinMode(JOYSTICK_Y, INPUT);
  13. }
  14.  
  15. void loop() {
  16.   int xValue = analogRead(JOYSTICK_X);
  17.   int yValue = analogRead(JOYSTICK_Y);
  18.  
  19.   // Turn off all LEDs
  20.   for (int i = 0; i < 9; i++) {
  21.     digitalWrite(LED_PINS[i], LOW);
  22.   }
  23.  
  24.   // Update position based on joystick input
  25.   if (xValue < 300 && currentPosition % 3 != 0) currentPosition--;
  26.   if (xValue > 700 && currentPosition % 3 != 2) currentPosition++;
  27.   if (yValue < 300 && currentPosition > 2) currentPosition -= 3;
  28.   if (yValue > 700 && currentPosition < 6) currentPosition += 3;
  29.  
  30.   // Turn on LED at current position
  31.   digitalWrite(LED_PINS[currentPosition], HIGH);
  32.  
  33.   delay(100); // Small delay for stability
  34. }
Tags: cogsworth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement