skizziks_53

Reddit dance pad attempt v2.0

Jun 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. /*
  2.   Reddit dance pad attempt (decounce issues) -- version 2.0
  3.   June 19, 2019
  4.  
  5.   Hardware: Leonardo implied (keyboard.h used)
  6.  
  7.   This sketch is changed to ONLY send a keypress and then a key-release each time the pad is pressed.
  8.   It doesn't send anything when the pad is released.
  9.  
  10. */
  11.  
  12. #include <Keyboard.h>
  13.  
  14.  
  15. int debounce_delay = 50; // This is the pad press-down delay time, in milliseconds.
  16.  
  17.  
  18.  
  19. int pad_input__pins[] = {4, 5, 6, 7, 8}; // upper left, lower left, center, upper right, lower right
  20. bool pad_enabled[] = {true, true, true, true, true};
  21. char pad_sendCaracters[] = {'r', 'v', 'g', 'y', 'n'}; // These characters correspond to pad_input__pins[]
  22. int input_current_status[] = {1, 1, 1, 1, 1}; // Since the pinmode is input-pullup, the normal pin states are high.
  23. int input_previous_status[] = {1, 1, 1, 1, 1};
  24. unsigned long input_begin_time[] = {0, 0, 0, 0, 0};
  25. unsigned long input_current_time[] = {0, 0, 0, 0, 0};
  26.  
  27.  
  28. void setup()
  29. {
  30.   for (int x = 0; x < 5; x++) {
  31.     pinMode(pad_input__pins[x], INPUT_PULLUP);
  32.   }
  33.   Keyboard.begin();
  34. }
  35.  
  36.  
  37.  
  38. void loop()
  39. {
  40.   for (int pLoop = 0; pLoop < 5; pLoop++) {
  41.     check_pad_input(pLoop);
  42.   }
  43. }
  44.  
  45.  
  46.  
  47. void check_pad_input(int dPad) {
  48.   if (pad_enabled[dPad] == true) {
  49.     input_current_status[dPad] = digitalRead(pad_input__pins[dPad]);
  50.     if (input_current_status[dPad] == 0) {
  51.       if (input_previous_status[dPad] == 1) {
  52.         // This should only happen once when the pad is pressed down.
  53.         Keyboard.press(pad_sendCaracters[dPad]);
  54.         Keyboard.releaseAll();
  55.         pad_enabled[dPad] = false;
  56.         input_begin_time[dPad] = millis();
  57.       }
  58.     }
  59.    
  60.   }
  61.   else {
  62.     input_current_time[dPad] = millis();
  63.     if (input_current_time[dPad] >= input_begin_time[dPad]) {
  64.       if (input_current_time[dPad] >= (input_begin_time[dPad] + debounce_delay)) {
  65.         pad_enabled[dPad] == true;
  66.       }
  67.     }
  68.     else {
  69.       input_begin_time[dPad] = millis();
  70.     }
  71.   }
  72.  
  73.   input_previous_status[dPad] = input_current_status[dPad];
  74. }
Add Comment
Please, Sign In to add comment