Debouncing - what is it, doing it in software

It was bugging me that I couldn't change the speed of the dance party effect for my workbench lighting, so I added the ability to adjust the beats per minute. I also converted the switch to an interrupt so that it behaved better while doing other work. Interrupts are a simple concept - stop whatever the device is doing and do this other thing right now. However, this also means I had to learn a bit about debouncing.

When an electrical circuit is closed by a button, the voltage fluctuates slightly until it flattens out. This fluctuation means that one button press can look like 3 or 4 button presses over the course of a couple hundred milliseconds. This behavior is called bouncing.

Debouncing uses either hardware to smooth out the voltage or software to ignore the bounces. Since I didn't have the components necessary for hardware debouncing, I used a software method that works pretty well. When the interrupt is triggered - the voltage from pressing the button rises, including from bouncing, check for when the last voltage rise was detected. If it's shorter than 200ms, ignore it. Debounced!