Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit dance pad attempt (decounce issues) -- version 2.0
- June 19, 2019
- Hardware: Leonardo implied (keyboard.h used)
- This sketch is changed to ONLY send a keypress and then a key-release each time the pad is pressed.
- It doesn't send anything when the pad is released.
- */
- #include <Keyboard.h>
- int debounce_delay = 50; // This is the pad press-down delay time, in milliseconds.
- 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]);
- Keyboard.releaseAll();
- pad_enabled[dPad] = false;
- input_begin_time[dPad] = millis();
- }
- }
- }
- 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();
- }
- }
- input_previous_status[dPad] = input_current_status[dPad];
- }
Add Comment
Please, Sign In to add comment