Depending on your experience with programming and electronics, you may have run into a situation where if you flip a switch, your device somehow thinks that you’ve switched the input multiple times.
This may seem strange, but when metal contacts hit each other, they tend to literally bounce on and off of each other for a short amount of time. This “bouncing time” can reportedly last between 10 and 300 microseconds, which as humans, we certainly don’t notice. You might consider that 300 microseconds (µs), expressed differently is 3 ten-thousandths of a second. It’s a fascinating topic and if you want to dive in you should check out the recent Spark Gap podcast episode on button types and debounce.
Though short to us, to modern computers and microcontrollers, this can be an eternity, allowing them to process many instructions. Though this speed presents a problem, it also presents solutions. As shown in this Arduino example, you can simply code things to wait until the state change has been experienced for a certain amount of time before recognizing it as such.
The disadvantage of this technique is that it requires extra code, making things less streamlined than they could be, and if you’re working with hardware-based logic, you’d need to use a different technique. There are solutions of course, including this NE555-based kit by Theosauro. It uses the button to trigger a 555 timer (that’s the NE555 chip) to output a very clean logic signal you just can’t get from a button alone.
So the next time your code is acting strange, consider adding a debounce function, either through hardware or software!