Bounce at each change
Latching does not stop a contact bouncing. At every change the metal touches and parts a few times before it settles, usually within a few milliseconds. A sketch that reads the level never notices; one that counts changes must wait for the level to hold.
One change, up close
Leave it on Counts changes and slide the setting up from nothing. The trace is one press, drawn over six milliseconds: SIGNAL goes HIGH, falls, rises, falls, and several changes later stays HIGH. It is illustrative. Nobody has recorded this switch, and its drawing does not say how long it bounces. Small mechanical switches usually settle within a few milliseconds, and that is the scale drawn.
It happens because a contact is two pieces of metal meeting under a spring. They hit, spring apart, hit again, and come to rest. A latch holds them together afterwards, but it does nothing about how they arrive. It happens again, the other way, when the next press opens them.
When it matters
It depends entirely on what the sketch does with the level.
| The sketch… | Does bounce matter? |
|---|---|
| reads the level and acts on it, like the first read | No. It is wrong for a few milliseconds and then right |
| decides once at start-up, like the mode sketch | No. Nobody is pressing it while the board starts |
| counts changes, or toggles something on each one | Yes. One press can count as several |
The first two are what this switch is best at, which is why most sketches for it never think about bounce. A sketch that copies the level to an LED copies the bounce too, and a few milliseconds of flicker is far too short to see.
Wait for the level to hold
The fix is the one a level, not an event already uses: believe a new level only once it has held for a while. Every bounce restarts the clock, so the level is accepted once, after the last bounce, when it has been still for 20 ms.
The wait has to be longer than the whole burst, not just longer than the gaps inside it. That is what the slider shows: a wait of half a millisecond still lets through a change that was never meant. 20 ms is several times any burst a small switch is likely to make, and far shorter than anyone can press twice.
When it does not work
Nobody has measured it, and the switch's drawing gives no figure. Small mechanical switches usually settle within a few milliseconds. The figure is a picture of the behaviour, not a recording, and the 20 ms in the sketches is chosen to be several times longer than any burst that is likely.
It can smooth the edges, but it adds a part for a job the sketch does for free, and it changes how fast SIGNAL rises and falls in a way the rest of the book does not account for. For a switch a person presses, the settle time in software is the simpler answer.
It reads the level twice a second and prints it. A read that lands inside a bounce might print the old position once more, and half a second later the next read prints the right one. Nothing is counted, so nothing is miscounted.
It only delays the report of a change by 20 ms, which nobody can see or feel. The switch's 700 gf press and its 50,000-cycle life are the real limits on how often it should be pressed, long before the settle time is.
The short list of reasons, in the order they are usually the answer.
When it reads wrong →Edit this page — content/books/latching-button/bounce-at-each-change.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Latching Button
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.