The problem with hardware is it
doesn't behave nicely. In the case of switches they don't just turn off and on. They bounce. When you push a button
it will go on, then off, then on, then off, before it goes on and stays on. The common way to deal with this is
to read a switch's input |
and if it has changed state you must ignore it for a fraction of a second. Just long enough for it to stop bouncing.
Then you may reliably read it as input again. |