A button does not close once. Here is what it actually does.
- 192parts on the bench
- 25boards running now
- 1.00xreal time, on every board
A tactile switch is a small metal dome that gets pushed down onto a contact. It does not land and stay still. It lands, springs back a fraction of a millimeter, lands again, and repeats that a few times before it settles, the same way a dropped coin does not stop the instant it first touches the table. Electrically that means the contact opens and closes several times in the first few milliseconds of a press, and again on release, before it holds steady.
Mokxi's pushbutton models this rather than hiding it. Every button on the bench bounces by default, on a profile called `typical`: three to eight transitions, settled somewhere between 1 and 5 milliseconds after the press starts. A `worst` profile goes up to twenty transitions over 10 to 20 milliseconds, which is closer to a tired switch or a long-levered limit switch, and a `none` profile is the ideal switch some circuits do not need to worry about. The button in the circuit above uses the default, so pressing it fires more than one edge exactly the way a real one does.
This is why a naive sketch that just counts edges on a button counts wrong: a single press can register as two, three, or more, depending on the switch and the moment. It is not a bug in the code reading it, it is the switch itself.
The fix in software costs almost nothing: after an edge, ignore further changes on that pin for a few milliseconds, using `millis()` rather than blocking with `delay()`. Somewhere between 5 and 20 milliseconds covers a typical switch comfortably, which lines up with the settling times above. A hardware fix does the same job with a small capacitor across the switch, which slows the voltage's rise and fall enough that the chatter never reaches a full logic transition; Mokxi's capacitor really does integrate over time rather than switching instantly, so this actually works here rather than being asserted. Debouncing inside an interrupt is the same idea again, with the added rule that the interrupt handler itself should do as little as possible: check the time, and get out.
The circuit above wires the button straight to a pin with the internal pull-up switched on, so you can watch the bounce with nothing else built. Open it, hold the button once, and if the serial monitor is showing anything counted per edge, watch the count move by more than one for a single press.
Want the step-by-step version? The lesson "Reading a button" walks through this with checkpoints.
Open the lessonBuild this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.