Can anyone break this?
[i]below this line will include latest updates[/i]
I was inspired to create this debouncer after reading this great debouncing guide, especially referring to the last plot of the microswitch on page 8, where it says "I found it usually generated a pulse train guaranteed to play havoc with simple filter code. There's no high speed hash, just hard-to-eliminate solid ones and zeroes."
This code monitors and cleans up any number of input signals (10 shown).
Edit: Updated - see reply#5
/*==========================================================================
• get stable readings on any number of input pins in buttons[] array
• no time-out to set after which false readings could occur
• minimum response 8+ ms (varies with signal quality)
• non blocking, no interrupts, fast response
• uses pattern recognition
• filters random noise to prevent false triggering
• detects rising and falling edges on normally high or normally low signals
• use on push buttons, switches, relays or digital inputs
buttonState: ______|________|_|_|_|??|????????|_|_|_|____________|_|________
buttonRead: _______________________|????????????????????????|________________
buttonState: ????|????????????|?|?|?|__|_________|_|??|?|?|??????????????????|???
buttonRead: ????????????????????????????????|___________________|???????????????
====================================================================dlloyd*/
// constants
const byte buttons[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // inputs
const byte qty = sizeof(buttons);
const byte led = 13;
// variables
byte buttonState[qty];
byte buttonRead[qty];
unsigned long microsStart;
void setup() {
pinMode(led, OUTPUT);
for (int i = 0; i < qty; i++)
{
pinMode(buttons[i], INPUT_PULLUP);
buttonState[i] = 0xFF;
buttonRead[i] = 1;
}
}
void loop() {
buttonFilter();
// your code starts here
digitalWrite(led, buttonRead[0]); // debounced and filtered status of pin 2
}
void buttonFilter(void)
{
if (micros() - microsStart >= 2000) // minimum interval between bounces = 2 ms
{
for (int i = 0; i < qty; i++)
{
buttonState[i] = (buttonState[i] << 1) | digitalRead(buttons[i]); // shift and read
if ((buttonState[i] & B11111) == B01111) // if rising and high for 3 stable reads
{
buttonRead[i] = 1;
}
if ((buttonState[i] & B11111) == B10000) // if falling and low for 3 stable reads
{
buttonRead[i] = 0;
}
}
}
microsStart = micros();
}
Example of pattern recognition within 1 byte (bits 5,6,7 are ignored):
[b]Button State Output Comments[/b]
00000000 LOW stable
000000[color=red][b]01[/b][/color] LOW noise <-- typical debouncer detects rising edge
000000[color=red][b]10[/b][/color] LOW noise <-- typical debouncer detects falling edge
000001[color=red][b]01[/b][/color] LOW noise <-- typical debouncer detects rising edge
00001011 LOW noise
00010111 LOW noise
001[color=teal][b]01111[/b][/color] HIGH [color=teal][b]rising[/b][/color]
01011111 HIGH stable
10111111 HIGH stable
01111111 HIGH stable
11111111 HIGH stable
111111[color=red][b]10[/b][/color] HIGH noise <-- typical debouncer detects falling edge
111111[color=red][b]01[/b][/color] HIGH noise <-- typical debouncer detects rising edge
111110[color=red][b]10[/b][/color] HIGH noise <-- typical debouncer detects falling edge
11110100 HIGH noise
11101000 HIGH noise
110[color=teal][b]10000[/b][/color] LOW [color=teal][b]falling[/b][/color]
10100000 LOW stable
01000000 LOW stable
10000000 LOW stable