Far lampeggiare un LED senza delay usando millis
Tenga traccia del tempo trascorso per eseguire le attività secondo programma.
Far lampeggiare un LED senza delay usando millis è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 3 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 Built-in Stopwatch
The millis function returns how many milliseconds your board has been running since it powered on. It is a free, always-counting stopwatch. ⏱️
unsigned long now = millis();Why unsigned long
Store millis in an unsigned long. The count grows huge over time, and a plain int would overflow in under a minute.
The Core Idea
Instead of pausing, you remember the last time you acted, then keep checking how much time has passed since then on every loop.
Remember the Last Moment
Keep a variable like previousMillis to store when you last toggled the LED. You will compare it against the current time each pass.
unsigned long previousMillis = 0;Set Your Interval
Choose an interval, say 1000 milliseconds, as the gap between blinks. Acting only after this much time passes gives a steady rhythm.
const long interval = 1000;Has Enough Time Passed
Each loop, subtract previousMillis from the current millis. If the gap is at least your interval, it is time to act.
if (millis() - previousMillis >= interval) {Reset the Timer
The instant you act, set previousMillis to the current time. This restarts the countdown so the next blink waits a full interval again.
previousMillis = millis();Flip the LED
Inside the timed block you simply toggle the LED, flipping it on if it was off and off if it was on, for a clean blink.
ledState = !ledState;
digitalWrite(ledPin, ledState);The Loop Never Stops
Between blinks the loop keeps spinning at full speed, free to read sensors or buttons. Nothing is ever frozen waiting.
Subtraction Handles Overflow
After about 49 days millis wraps to zero, but the subtraction trick still gives the right gap, so your timing keeps working.
A Reusable Pattern
This millis pattern works for any repeating task, not just blinking. Change the interval and the action and you can schedule anything.
Quick Check
Why do we store the result of millis in an unsigned long?
Recap
You learned the millis pattern: remember the last time, check the elapsed gap, then act and reset. Now your board blinks while staying fully alert. 🎉
Domande Frequenti
La lezione «Far lampeggiare un LED senza delay usando millis» è gratuita?
Sì — il testo completo di «Far lampeggiare un LED senza delay usando 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 «Far lampeggiare un LED senza delay usando millis»?
Tenga traccia del tempo trascorso per eseguire le attività secondo programma. 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 3 di 4.
Quanto tempo richiede la lezione «Far lampeggiare un LED senza delay usando 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
- int, float, bool su Arduino
- Perché delay() blocca tutto
- Far lampeggiare un LED senza delay usando millis
- Eseguire due attività contemporaneamente