The circuit
The button sits between pin 2 and ground. A 10 kΩ pull-up resistor ties the pin to 5 V, as in the lesson on pull-up resistors. Released, nothing joins the pin to ground, so digitalRead gives HIGH: 1.
Press the button and its contacts join the pin straight to ground. Now the pin reads LOW: 0. Pressed means LOW here. That is called negative logic. So we expect one clean change, 1 to 0, for one press.
The problem: bounce
Metal contacts are springy. They hit, jump apart and hit again before they settle. In our example the pin falls to LOW 4 times in 3.1 ms. Ganssle measured 1.557 ms of bounce on average.
At 16 MHz one millisecond is 16,000 clock ticks. loop() runs many times inside each bounce, so digitalRead catches every short LOW and every short HIGH.
This first loop() adds 1 each time the pin changes from HIGH to LOW. Our one press has 4 such changes, so count jumps to 4. A menu would skip 3 items, or a light would flick on and off.
The fix: wait for quiet
Debouncing means ignoring the bounce. The new loop() takes a reading only once it has stayed the same for more than 50 ms. 50 ms is the delay in Arduino's own Debounce example.
Line 3 notes the time, millis(), whenever the reading changes. The quiet time is millis() minus that note. Every bounce resets it to 0, so during the bounce it never gets near 50.
After the last bounce the pin stays LOW. At 54 ms the quiet time passes 50, so line 7 takes the new state and line 8 adds 1. One press, count 1.
Long enough: Ganssle found most switches bounce well under 10 ms. Short enough: in his test a 100 ms delay felt slow, but 50 ms seemed instant. Releasing the button bounces too, and the same rule cleans it.
What happens
// step 1/9 · circuit board Arduino Uno R3 (ATmega328P) pin 2 pinMode(BTN, INPUT), 10 kOhm pull-up to 5 V button from pin 2 to GND released pin pulled up to 5 V -> HIGH (1)
The bounce in the figure is one example, drawn to scale as clean steps: 4 touches in 3.1 ms. Real contacts also give fuzzy voltages in between, and every press is different. Ganssle's 18 switches averaged 1557 µs, but two of them bounced for over 6.2 ms. The card shows only loop(). The whole sketch also declares last, state, t0 and count, and sets pinMode(BTN, INPUT) in setup(), because the 10 kΩ resistor is outside the chip. With no resistor you would use INPUT_PULLUP, which turns on the chip's own pull-up.