Debounce multiple push buttons, switches, relays or digital signals


can break this?
below line include latest updates


i inspired create debouncer after reading this great debouncing guide, referring last plot of microswitch on page 8, says "i found generated pulse train guaranteed play havoc simple filter code. there's no high speed hash, hard-to-eliminate solid ones , zeroes."

this code monitors , cleans number of input signals (10 shown).
edit: updated - see reply#5
code: [select]

/*==========================================================================
• stable readings on number of input pins in buttons[] array
• no time-out set after false readings occur
• minimum response 8+ ms (varies signal quality)
• non blocking, no interrupts, fast response
• uses pattern recognition
• filters random noise prevent false triggering
• detects rising , falling edges on high or 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 = 0; < qty; i++)
 {
   pinmode(buttons[i], input_pullup);
   buttonstate[i] = 0xff;
   buttonread[i] = 1;
 }
}

void loop() {
 buttonfilter();
 // code starts here
 digitalwrite(led, buttonread[0]); // debounced , filtered status of pin 2
}

void buttonfilter(void)
{
 if (micros() - microsstart >= 2000) // minimum interval between bounces = 2 ms
 {
   for (int = 0; < qty; i++)
   {
     buttonstate[i] = (buttonstate[i] << 1) | digitalread(buttons[i]); // shift , read
     if ((buttonstate[i] & b11111) == b01111) // if rising , high 3 stable reads
     {
       buttonread[i] = 1;
     }
     if ((buttonstate[i] & b11111) == b10000) // if falling , low 3 stable reads
     {
       buttonread[i] = 0;
     }
   }
 }
 microsstart = micros();
}


example of pattern recognition within 1 byte (bits 5,6,7 ignored):

button state  output  comments
00000000      low     stable
00000001      low     noise   <-- typical debouncer detects rising edge
00000010      low     noise   <-- typical debouncer detects falling edge
00000101      low     noise   <-- typical debouncer detects rising edge
00001011      low     noise
00010111      low     noise
00101111      high    rising
01011111      high    stable
10111111      high    stable
01111111      high    stable
11111111      high    stable
11111110      high    noise   <-- typical debouncer detects falling edge
11111101      high    noise   <-- typical debouncer detects rising edge
11111010      high    noise   <-- typical debouncer detects falling edge
11110100      high    noise
11101000      high    noise
11010000      low     falling
10100000      low     stable
01000000      low     stable
10000000      low     stable

is there break ? please explain, question ?


Arduino Forum > Using Arduino > Programming Questions > Debounce multiple push buttons, switches, relays or digital signals


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support