Eseguire il debounce con millis()
Ignori il rumore usando una breve finestra di stabilità.
Eseguire il debounce con millis() è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Not Just delay()?
You could pause with delay() after a press, but that freezes your whole board. The smarter tool for clean buttons is millis().
millis() Is a Stopwatch
millis() returns how many milliseconds your board has been running. You compare two readings to measure elapsed time without stopping.
unsigned long now = millis();The Debounce Plan
The idea is simple: when the pin changes, start a timer. Only trust the new state if it holds steady past a short window.
Pick a Debounce Window
A delay of about 50 milliseconds is the classic choice. It's long enough to outlast bounce, yet far too fast for you to notice.
const unsigned long DEBOUNCE = 50;Remember the Last Change
You need a variable to store when the pin last flipped. Compare it to the current time on every loop pass.
unsigned long lastChange = 0;Track Two States
Keep the raw reading and the confirmed stable state separately. The raw one is noisy; the stable one is what your logic trusts.
int lastRaw = HIGH;
int stable = HIGH;Detect a Flip
Each loop, read the pin. If it differs from the last raw value, the signal just moved, so reset your timer.
int raw = digitalRead(BTN);
if (raw != lastRaw)
lastChange = millis();Wait for Stillness
If enough time has passed since the last flip, the reading has settled. Now it is safe to accept it as the real state.
if (millis() - lastChange > DEBOUNCE) {
stable = raw;
}Act on the Edge
To fire once per press, watch for the stable state changing to LOW. That single edge is your clean, debounced press.
if (stable == LOW && lastStable == HIGH)
Serial.println("clean press");Update for Next Time
At the loop's end, save the current readings into your last variables. This keeps the comparison fresh on every pass.
lastRaw = raw;
lastStable = stable;Non-Blocking Wins
Because millis() never pauses the board, your sensors and outputs keep running. You debounce while everything else stays alive. 🚀
Quick Check
Let's check how millis() debouncing decides a press is real.
Recap: Clean Presses
You used millis() to ignore bounce without freezing the board, accepting a press only once it settles. Solid, professional input! ✅
Domande Frequenti
La lezione «Eseguire il debounce con millis()» è gratuita?
Sì — il testo completo di «Eseguire il debounce con millis()» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Cosa imparerò in «Eseguire il debounce con millis()»?
Ignori il rumore usando una breve finestra di stabilità. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Arduino & IoT Academy?
Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Eseguire il debounce con millis()»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?
Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché i pulsanti hanno il “rimbalzo”
- Eseguire il debounce con millis()
- Salvare un valore in EEPROM
- Ricordare un contatore dopo il reset