skizziks_53

Reddit dance pad attempt v1.0

Jun 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. /*
  2.   Reddit dance pad attempt (decounce issues)
  3.   June 19, 2019
  4.  
  5.   Hardware: Leonardo implied (keyboard.h used)
  6.  
  7.  
  8. */
  9.  
  10. #include <Keyboard.h>
  11.  
  12.  
  13. int debounce_delay = 50; // This is the pad press-down delay time, in milliseconds.
  14.  
  15. bool use_delayed_release = false; // This is an option to allow the release function to work even if the pad input is still debouncing.
  16. //                                   If this doesn't work well set to false, you can try setting it to true instead.
  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.         pad_enabled[dPad] = false;
  55.         input_begin_time[dPad] = millis();
  56.       }
  57.     }
  58.     if (use_delayed_release == true) {
  59.       // This release detetion only works if the pad is enabled (after the debounce delay has passed).
  60.       if (input_current_status[dPad] == 1) {
  61.         if (input_previous_status[dPad] == 0) {
  62.           // This should only happen once when the pad is released.
  63.           Keyboard.release(pad_sendCaracters[dPad]);
  64.         }
  65.       }
  66.     }
  67.   }
  68.   else {
  69.     input_current_time[dPad] = millis();
  70.     if (input_current_time[dPad] >= input_begin_time[dPad]) {
  71.       if (input_current_time[dPad] >= (input_begin_time[dPad] + debounce_delay)) {
  72.         pad_enabled[dPad] == true;
  73.       }
  74.     }
  75.     else {
  76.       input_begin_time[dPad] = millis();
  77.     }
  78.   }
  79.   if (use_delayed_release == false) {
  80.     //This release detection will work at any time, even while the pad is still disabled while decouncing.
  81.     // If the pad is debouncing, this will also re-enable the pad.
  82.     if (input_current_status[dPad] == 1) {
  83.       if (input_previous_status[dPad] == 0) {
  84.         // This should only happen once when the pad is released.
  85.         Keyboard.release(pad_sendCaracters[dPad]);
  86.         pad_enabled[dPad] == true;
  87.       }
  88.     }
  89.   }
  90.   input_previous_status[dPad] = input_current_status[dPad];
  91. }
Add Comment
Please, Sign In to add comment