Ricordare un contatore dopo il reset
Mantenga lo stato tra un ciclo di alimentazione e l'altro.
Ricordare un contatore dopo il reset è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 4 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.
A Counter That Survives
Let's build a press counter that remembers its total even after a power cut. We'll combine debouncing with EEPROM storage.
The Plan in One Line
On startup, load the saved count. On each clean press, add one and save it back. Simple, but powerful.
Pick a Storage Slot
Reserve one EEPROM address for the counter. We'll keep it small enough to fit in a single byte for now.
const int ADDR = 0;
byte count = 0;Load on Boot
Inside setup(), read the saved value first. Now your counter picks up exactly where it left off.
void setup() {
count = EEPROM.read(ADDR);
}Wipe the Junk First
A fresh chip's EEPROM often reads 255. Treat that as empty and reset your counter to zero on the first run.
if (count == 255) count = 0;Reuse Your Debounce
Detect presses with the millis() debounce from before. Clean input is essential, or your saved count drifts upward.
Increment on a Clean Press
When a debounced press fires, bump the counter by one. This is the single moment your total grows.
count++;
Serial.println(count);Save Without Wearing Out
Use EEPROM.update to write the new total. It only touches memory when the value actually changed, protecting the chip.
EEPROM.update(ADDR, count);Test the Magic
Press a few times, then unplug and replug the board. The Serial Monitor shows your count continuing, not restarting. 🎉
Mind the Byte Limit
A byte counter wraps back to 0 after 255. For bigger totals, store an int with EEPROM.put and EEPROM.get instead.
Add a Reset Option
A real product often needs a way to clear the saved count. A long button hold could write 0 back to EEPROM.
EEPROM.update(ADDR, 0);Quick Check
Let's check the key step that lets the counter survive a reset.
Recap: Memory Across Resets
You built a counter that persists by loading from EEPROM at boot and saving each clean press. Your device finally has a memory! 🧠
Domande Frequenti
La lezione «Ricordare un contatore dopo il reset» è gratuita?
Sì — il testo completo di «Ricordare un contatore dopo il reset» è 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 «Ricordare un contatore dopo il reset»?
Mantenga lo stato tra un ciclo di alimentazione e l'altro. 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 4 di 4.
Quanto tempo richiede la lezione «Ricordare un contatore dopo il reset»?
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