Debouncing dance pad keyboard input device

Hi!

I've built a Stepmania PC rythm game dance pad following this tutorial:

It's a Leonardo USB keyboard input device using pull-up resistors and some very rudimentary code. I have a problem with debouncing the input for use in applications other than Stepmania (since the game handles debouncing through its options), so that single steps do not register as more than one press. I've watched and read various tutorials on the subject, but since my experience in programming is practically non-existent, it's all way over my head.

I would be most grateful if anyone could show me how to modify the code I'm using to include debouncing of e.g. 100 ms for the button presses.

/* DDR USB Dance Pad Code - Super Make Something Episode 9) - https://youtu.be/-qeD2__yK4c
 * by: Alex - Super Make Something
 * date: June 1st, 2016
 * license: Creative Commons - Attribution - Non-Commercial.  More information available at: http://creativecommons.org/licenses/by-nc/3.0/
 */

/*
 * This code contains the follow functions:
 * - void setup(): Sets pins 10, 3, 6, 15 to input with pull-up resistors enabled and begins Keyboard functionality
 * - void loop(): Main loop - reads pin voltages and sends out corresponding keystrokes via USB
 */

/* 
 * Pinout:
 * - "HIGH" voltage button contacts - pins 10, 3, 6, 15
 * - "GND" voltage button contacts - GND pin
 */

#include <Keyboard.h>

int upStatus=1;
int upStatusPrev=1;
int leftStatus=1;
int leftStatusPrev=1;
int downStatus=1;
int downStatusPrev=1;
int rightStatus=1;
int rightStatusPrev=1;

void setup()
{
  pinMode(10,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(15,INPUT_PULLUP);
  Keyboard.begin();
}

void loop()
{
 
 upStatus=digitalRead(10);
 leftStatus=digitalRead(3);
 downStatus=digitalRead(6);
 rightStatus=digitalRead(15);
 
 //UP ARROW PRESSED
 if (upStatus!=upStatusPrev && upStatus==LOW)
 {
    Keyboard.press('i');
    upStatusPrev=upStatus;
 }
 //UP ARROW RELEASED
 if (upStatus!=upStatusPrev && upStatus==HIGH)
 {
    Keyboard.release('i');
    upStatusPrev=upStatus;
 }
 
  //LEFT ARROW PRESSED
 if (leftStatus!=leftStatusPrev && leftStatus==LOW)
 {
    Keyboard.press('j');
    leftStatusPrev=leftStatus;
 }
 //LEFT ARROW RELEASED
 if (leftStatus!=leftStatusPrev && leftStatus==HIGH)
 {
    Keyboard.release('j');
    leftStatusPrev=leftStatus;
 }
 
  //DOWN ARROW PRESSED
 if (downStatus!=downStatusPrev && downStatus==LOW)
 {
    Keyboard.press('k');
    downStatusPrev=downStatus;
 }
 //DOWN ARROW RELEASED
 if (downStatus!=downStatusPrev && downStatus==HIGH)
 {
    Keyboard.release('k');
    downStatusPrev=downStatus;
 }
 
 //RIGHT ARROW PRESSED
 if (rightStatus!=rightStatusPrev && rightStatus==LOW)
 {
    Keyboard.press('l');
    rightStatusPrev=rightStatus;
 }
 //RIGHT ARROW RELEASED
 if (rightStatus!=rightStatusPrev && rightStatus==HIGH)
 {
    Keyboard.release('l');
    rightStatusPrev=rightStatus;
 }
}

You could do it yourself but it would be easier to search for and use the Bounce2 library.

I'd use arrays and a for loop, as well as a simple class for debouncing buttons.

Code:

---



```
#include "Button.h"
#include <Keyboard.h>

Button buttons[4] = { 10, 3, 6, 15 };
const char keys[4] = { 'i', 'j', 'k', 'l' };

void setupcolor=#000000[/color] {
 Keyboard.begincolor=#000000[/color];
}

void loopcolor=#000000[/color] {
 for (uint8_t i = 0; i < 4; i++) {
   Button::State state = buttons[i].getStatecolor=#000000[/color];
   if (state == Button::Falling)
     Keyboard.presscolor=#000000[/color];
   else if (state == Button::Rising)
     Keyboard.releasecolor=#000000[/color];
 }
}
```

|

You need the Button.h and Button.cpp files in the .ZIP file.

Pieter

Button-keyboard.zip (1.83 KB)

Thank you both for your answers! Ultimately I went with Pieter's code and it works exactly as I needed. Much appreciated!