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;
}
}