Polling contro interrupt
Scopra perché alcuni eventi non possono attendere il loop.
Polling contro interrupt è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 1 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.
Always Checking
So far your loop uses polling: it runs around and around, asking each sensor and button over and over whether anything changed.
Polling in Action
A typical poll reads a pin every pass of the loop. If the value matches what you want, you react; otherwise you simply keep looping. 🔁
if (digitalRead(buttonPin) == LOW) {
doSomething();
}The Hidden Cost
Polling works, but it wastes effort asking when nothing has changed. Your board burns cycles checking a pin that stays the same for a long time.
When the Loop Is Busy
If a slow task or a long delay holds the loop, a quick press can come and go before you ever read the pin again.
Events That Cannot Wait
Some signals are fast and brief: a sensor spike, an encoder edge, a short pulse. Miss the moment and the event is gone for good.
A Smarter Way
An interrupt flips the model: instead of you asking, the hardware taps your shoulder the instant something happens on a pin.
Drop Everything
When an interrupt fires, the chip pauses whatever the loop was doing, runs your handler, then returns exactly where it left off. ⚡
Never Miss the Moment
Because the hardware watches the pin for you, even a tiny pulse triggers your code. You catch events a slow loop would skip.
Free Up the Loop
With interrupts handling urgent signals, your loop stays clean and responsive, free to do its slower work without constant checking.
When Polling Is Fine
Not everything needs an interrupt. For a button a human presses slowly, polling is simpler and perfectly good enough.
Choosing the Right Tool
Reach for an interrupt when an event is fast, brief, or might arrive while the loop is busy. Otherwise polling keeps things easy.
Quick Check
Which situation is the best reason to choose an interrupt over polling?
Recap
You saw that polling keeps asking while an interrupt gets notified. Use interrupts for fast events and polling for relaxed ones. 🎉
Domande Frequenti
La lezione «Polling contro interrupt» è gratuita?
Sì — il testo completo di «Polling contro interrupt» è 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 «Polling contro interrupt»?
Scopra perché alcuni eventi non possono attendere il loop. 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 1 di 4.
Quanto tempo richiede la lezione «Polling contro interrupt»?
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
- Polling contro interrupt
- Usare attachInterrupt su un pin
- Scrivere una ISR sicura
- Contare gli impulsi di un encoder