I now see that I'm not quite accurate as to what I thought the debounce code is doing
in my program; I said,
It repeatedly reports that the key is pressed (if it's still held down), instead of just reporting it once.
But actually for some keys it does NOT cause repeated actions on hold the key down, it only does this for keys whose actions take more than a little bit of time (like outputting speech from WaveShield), which may, I guess, be taking longer than the 100 ms debounce time specified in the program, so when the key-pin is looked at next, it may seem to have been re-pressed, I guess.
I'm now trying to figure out exactly what this debounce code does, so I might alter it to work as I want.
Here's the best I have been able to figure it out so far, any corrections/additions would be appreciated!
(What I'd like to be able to do with it is cut the debounce part out of the scan switches part into a separate function, so I could call it more than once for checking for double-taps, and I'm not sure how to make just the debounce part as a function, passing necessary parameters, etc.)
Code: Select all
byte check_switches()
{
static byte previous[4]; // array of previous state of button input pins
static long time[4]; // array of a point in time, reset for & on each keys pressing
byte reading; // a momentary reading of the state of a button pin
byte pressed; // a return of the PIN NUMBER for a button that has been pressed
byte index; // a zero based counter for the for-loop, used to access the (zero based) arrays
pressed = 0; // if no button pressed, return zero
for (byte index = 0; index < 4; ++index) { // loop through the button pin array, from 0 to 3
reading = digitalRead(14 + index); // read each pin in succession as loop progresses
// THIS IS WHERE THE DEBOUNCE OCCURS:
// it looks to see if the CURRENT immediate state of an button input pin is LOW,
// AND the last(?) known recorded state of that pin was HIGH,
// AND this moment in time (millis()) minus the TIME of the LAST time the key wasn't pressed???? (not sure about this)
if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
{
// switch has been pressed
time[index] = millis(); // saving the moment in time this button was pressed
pressed = index + 1; // accounting for zero-based array index
break; // exiting from for loop
}
previous[index] = reading; // not sure why this is HERE, and not ALSO just before the <break>???
}
// return switch number (1 - 4)
return (pressed);
}