If a button does not bounce on press, you get the below states
Pressed no bounce
0000000000000001 1
0000000000000011 3
0000000000000111 7
0000000000001111 F
0000000000011111 1F
0000000000111111 3F
0000000001111111 7F
0000000011111111 FF
0000000111111111 1FF
0000001111111111 3FF
0000011111111111 7FF
0000111111111111 FFF
0001111111111111 1FFF
0011111111111111 3FFF
0111111111111111 7FFF
This is a perfect sequence.
If a button bounces on press, you het e.g. the below sequence (<< indicates the first bounce)
Pressed with bounce
0000000000000001 0x1
0000000000000010 0x2 <<
0000000000000100 0x4
0000000000001000 0x8
0000000000010001 0x11
0000000000100011 0x23
0000000001000111 0x47
0000000010001111 0x8F
0000000100011111 0x11F
0000001000111111 0x23F
0000010001111111 0x47F
0000100011111111 0x8FF
0001000111111111 0x11FF
0010001111111111 0x23FF
0100011111111111 0x47FF
1000111111111111 0x8FFF
0001111111111111 0x1FFF
0011111111111111 0x3FFF
0111111111111111 0x7FFF
1111111111111111 0xFFFF
The sequence will eventually reach 0xFFFF but it takes a bit longer.
If a button does not bounce on release, you get the below states (the * before 0xFFF0 indicates that the function will return true)
Released no bounce
1111111111111111 FFFF
1111111111111110 FFFE
1111111111111100 FFFC
1111111111111000 FFF8
1111111111110000 *FFF0
1111111111100000 FFE0
1111111111000000 FFC0
1111111110000000 FF80
1111111100000000 FF00
1111111000000000 FE00
1111110000000000 FC00
1111100000000000 F800
1111000000000000 F000
1110000000000000 E000
1100000000000000 C000
1000000000000000 8000
0000000000000000 0
Again a perfect sequence.
If a button bounces on release, you can get the below states (<< indicates the first bounce)
Released with bounce
1111111111111111 FFFF
1111111111111110 FFFE
1111111111111101 FFFD <<
1111111111111011 FFFB
1111111111110111 FFF7
1111111111101110 FFEE
1111111111011100 FFDC
1111111110111000 FFB8
1111111101110000 FF70
1111111011100000 FEE0
1111110111000000 FDC0
1111101110000000 FB80
1111011100000000 F700
1110111000000000 EE00
1101110000000000 DC00
1011100000000000 B800
0111000000000000 7000
1110000000000000 E000
1100000000000000 C000
1000000000000000 8000
0000000000000000 0
Note that the state will never become 0xFFF0.
Note:
0xFFFF and 0x0000 (the last line in each block) will be repeated endlessly till the button changes from pressed to released or vice versa.
The modified debounce function that will show the above sequences
bool debounce(void)
{
static uint16_t btnState = 0;
static uint16_t oldState = 0;
btnState = (btnState << 1) | !digitalRead(BTN_PIN) /* | 0xE000 */;
if (btnState != oldState)
{
oldState = btnState;
printBin16(btnState);
//Serial.println(btnState, BIN);
if (btnState == 0xFFF0) Serial.print(" *");
else Serial.print(" ");
Serial.println(btnState, HEX);
}
return (btnState == 0xFFF0);
}
And the function to print in binatry with leading zeroes
void printBin16(uint16_t val)
{
uint16_t x = 0x8000;
while (x > 1)
{
if (val < x) Serial.print("0");
x >>= 1;
}
Serial.print(val, BIN);
}