Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit dance pad attempt (decounce issues)
- June 19, 2019
- Hardware: Leonardo implied (keyboard.h used)
- */
- #include <Keyboard.h>
- int debounce_delay = 50; // This is the pad press-down delay time, in milliseconds.
- bool use_delayed_release = false; // This is an option to allow the release function to work even if the pad input is still debouncing.
- // If this doesn't work well set to false, you can try setting it to true instead.
- int pad_input__pins[] = {4, 5, 6, 7, 8}; // upper left, lower left, center, upper right, lower right
- bool pad_enabled[] = {true, true, true, true, true};
- char pad_sendCaracters[] = {'r', 'v', 'g', 'y', 'n'}; // These characters correspond to pad_input__pins[]
- int input_current_status[] = {1, 1, 1, 1, 1}; // Since the pinmode is input-pullup, the normal pin states are high.
- int input_previous_status[] = {1, 1, 1, 1, 1};
- unsigned long input_begin_time[] = {0, 0, 0, 0, 0};
- unsigned long input_current_time[] = {0, 0, 0, 0, 0};
- void setup()
- {
- for (int x = 0; x < 5; x++) {
- pinMode(pad_input__pins[x], INPUT_PULLUP);
- }
- Keyboard.begin();
- }
- void loop()
- {
- for (int pLoop = 0; pLoop < 5; pLoop++) {
- check_pad_input(pLoop);
- }
- }
- void check_pad_input(int dPad) {
- if (pad_enabled[dPad] == true) {
- input_current_status[dPad] = digitalRead(pad_input__pins[dPad]);
- if (input_current_status[dPad] == 0) {
- if (input_previous_status[dPad] == 1) {
- // This should only happen once when the pad is pressed down.
- Keyboard.press(pad_sendCaracters[dPad]);
- pad_enabled[dPad] = false;
- input_begin_time[dPad] = millis();
- }
- }
- if (use_delayed_release == true) {
- // This release detetion only works if the pad is enabled (after the debounce delay has passed).
- if (input_current_status[dPad] == 1) {
- if (input_previous_status[dPad] == 0) {
- // This should only happen once when the pad is released.
- Keyboard.release(pad_sendCaracters[dPad]);
- }
- }
- }
- }
- else {
- input_current_time[dPad] = millis();
- if (input_current_time[dPad] >= input_begin_time[dPad]) {
- if (input_current_time[dPad] >= (input_begin_time[dPad] + debounce_delay)) {
- pad_enabled[dPad] == true;
- }
- }
- else {
- input_begin_time[dPad] = millis();
- }
- }
- if (use_delayed_release == false) {
- //This release detection will work at any time, even while the pad is still disabled while decouncing.
- // If the pad is debouncing, this will also re-enable the pad.
- if (input_current_status[dPad] == 1) {
- if (input_previous_status[dPad] == 0) {
- // This should only happen once when the pad is released.
- Keyboard.release(pad_sendCaracters[dPad]);
- pad_enabled[dPad] == true;
- }
- }
- }
- input_previous_status[dPad] = input_current_status[dPad];
- }
Add Comment
Please, Sign In to add comment