You press a button once. Your sketch sees thirty presses. This isn't a bug in your code — it's physics. Mechanical contacts bounce as they close, oscillating between connected and disconnected for a few milliseconds. The chip sees every one of those edges as a separate press.

The fix is patience

The standard debounce pattern: every time the raw pin state changes, note the time. Don't accept a new "official" state until the raw state has been stable for some threshold (usually 20–50 ms). Bounces are over by 10 ms; a real user can't press twice in 50 ms.

The code is ten lines: track the last-known stable state, the last time the raw pin changed, and a debounce delay. On every loop, compare the raw state to the last seen; if they differ, reset the timer; if they've matched for longer than the threshold, commit the new state as official. Increment your counter only on the official transition.

Why it matters everywhere

Every keyboard you've ever owned does this in firmware. Every elevator button. Every game controller. The threshold isn't a tunable luxury — it's the difference between counting "one floor" and counting "twelve." A microcontroller is fast enough to be confused by a switch closure; the software has to slow itself down on purpose.