0Pricing
Arduino & IoT Academy · Lezione

Eseguire due attività contemporaneamente

Faccia lampeggiare un LED e legga un pulsante simultaneamente.

Eseguire due attività contemporaneamente è 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.

The Multitasking Dream

Real devices do many things at once: blink a light while watching a button. With non-blocking timing your board can finally juggle both.

Each Task Gets Its Timer

The trick is one timer per task. Give the blink its own previousMillis and interval, separate from anything else you want to run.

Track Time Separately

Keep a dedicated previousMillis for the LED so its schedule never tangles with other timed jobs in the same loop.

unsigned long ledPrevious = 0;
const long ledInterval = 500;

Blink Block

First, the blink block checks its own timer. When 500 milliseconds pass it toggles the LED and resets, then control moves on.

if (millis() - ledPrevious >= ledInterval) {
  ledPrevious = millis();
}

Read the Button Every Loop

Right after the blink block, you read the button on every single pass. Because nothing froze, no press is ever missed.

int state = digitalRead(buttonPin);

Two Jobs, One Loop

Now the same loop handles both: it times the blink and checks the button thousands of times a second. They feel simultaneous.

Add a Third Task

Want more? Just add another timer and block, like reading a sensor every two seconds. The pattern scales to many tasks cleanly.

Keep the Loop Fast

The rule that makes it work: never block. Keep every part of the loop quick so it can return often and serve all tasks fairly.

Not True Parallelism

The board still does one thing at a time, just very fast. This is cooperative multitasking, where each task does a little and yields.

Banish the Big delay

One long delay anywhere breaks the whole illusion, since it freezes every task. Replace pauses with millis checks to keep things smooth.

The Heart of Responsive IoT

This state machine style, many small timed checks in one loop, is how responsive sensors, displays, and connected devices are really built.

Quick Check

How does one loop blink an LED and read a button at the same time?

Recap

You combined timers in one loop to multitask: blink and read a button together, with no blocking delay. That is the core skill behind real IoT devices. 🚀

Domande Frequenti

La lezione «Eseguire due attività contemporaneamente» è gratuita?

Sì — il testo completo di «Eseguire due attività contemporaneamente» è 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 due attività contemporaneamente»?

Faccia lampeggiare un LED e legga un pulsante simultaneamente. 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 «Eseguire due attività contemporaneamente»?

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

  1. int, float, bool su Arduino
  2. Perché delay() blocca tutto
  3. Far lampeggiare un LED senza delay usando millis
  4. Eseguire due attività contemporaneamente
← Torna a Arduino & IoT Academy