if/else: LED Follows the Button
Light an LED only while the button is held.
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
}All lessons in this course
- INPUT vs INPUT_PULLUP
- Reading a Button State
- if/else: LED Follows the Button
- Wire a Button on a Breadboard