Advertisement
iSach

Joystick-controlled LEDs

May 13th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. int led_pins[] = {11, 13, 2, 3, 4, 5, 6, 7};
  2. const int pin_count = sizeof(led_pins) / sizeof (led_pins[0]);
  3. int joy_pin = A0;
  4.  
  5. void setup() {
  6. for (int i = 0; i < pin_count; i++) {
  7. pinMode(led_pins[i], OUTPUT);
  8. }
  9.  
  10. Serial.begin(9600);
  11. }
  12.  
  13. void loop() {
  14. int joystick_input = analogRead(joy_pin);
  15. int converted_joy_input = joystick_input / (1000 / (pin_count - 1));
  16.  
  17. int led_to_light = led_pins[converted_joy_input];
  18.  
  19. Serial.println(converted_joy_input);
  20.  
  21. for (int i = 0; i < pin_count; i++) {
  22. if(i != converted_joy_input) {
  23. digitalWrite(led_pins[i], LOW);
  24. }
  25. }
  26. digitalWrite(led_to_light, HIGH);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement