if/else: il LED segue il pulsante
Accendere un LED solo mentre si tiene premuto il pulsante
if/else: il LED segue il pulsante è 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.
Make a Decision
Reading a button is great, but the magic is reacting to it. An if statement lets your board choose what to do based on the reading.
if Runs Conditionally
The code inside if only runs when its test is true. So you can light an LED only when the button is actually pressed.
if (state == LOW) {
// button is pressed
}Test for Pressed
Remember the inversion: with INPUT_PULLUP, pressed is LOW. So your condition checks whether the stored state equals LOW.
int state = digitalRead(2);
if (state == LOW) {
digitalWrite(13, HIGH);
}Add an else Branch
What about when it isn't pressed? The else block runs whenever the if test is false, giving you the other half of the behavior.
if (state == LOW) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}LED Follows the Button
Together if and else make the LED mirror the button: on while held, off when released. That is your first real input-to-output link. 💡
Set Up Both Pins
The button pin is an input and the LED pin is an output. Declare both modes in setup() so each pin knows its job.
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}One Equals vs Two
A classic trap: use == to compare, not a single =. One equals assigns a value, while two equals asks if they match.
Loop Keeps It Live
Put the whole thing in loop() so it checks the button thousands of times a second. The LED reacts the moment you press.
Read Then Decide
The pattern is always the same: read the input, then decide with if/else. Get comfortable with it; you'll reuse it everywhere.
Compare Right in the if
You can skip the variable and test the read directly. It reads cleanly and does exactly the same comparison inside the parentheses.
if (digitalRead(2) == LOW) {
digitalWrite(13, HIGH);
}From Sensing to Acting
You just closed the loop from input to output with pure logic. Swap the LED for a buzzer or motor and the same idea still drives it.
Quick Check
Pick the condition that lights the LED while the button is held.
Recap
You used if/else to make an LED follow a button: LOW lights it, HIGH turns it off. Remember == for comparison. Next you'll wire it all up. 🔌
Domande Frequenti
La lezione «if/else: il LED segue il pulsante» è gratuita?
Sì — il testo completo di «if/else: il LED segue il pulsante» è 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 «if/else: il LED segue il pulsante»?
Accendere un LED solo mentre si tiene premuto il pulsante 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 «if/else: il LED segue il pulsante»?
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
- INPUT e INPUT_PULLUP
- Leggere lo stato di un pulsante
- if/else: il LED segue il pulsante
- Collegare un pulsante su una breadboard