Until now, our programs have been like a shopping list: “Do this, then wait, then do that.” The Arduino obeyed in order, line by line.
But true intelligence emerges when we are able to adapt to circumstances.
We don’t always want the LED to blink. Maybe we want it to turn on ONLY IF we press a button, or to turn off IF there is too much light.
Today we are going to learn the most fundamental structure in programming: the conditional statement if / else (this is the moment your Arduino learns to make decisions).
The Concept: A Fork in the Road
Imagine you are driving and you come to a fork in the road. You have to make a decision based on a condition:
- IF I have gas, I keep driving on the highway.
- IF NOT, I turn off to the gas station.
In programming, this works exactly the same way. We give the Arduino a condition (a Yes or No question). If the answer is Yes, it executes a block of code. If it’s No, it ignores it (or does something else).
The Syntax
The structure in C++ (Arduino’s language) is quite intuitive:
if (condition) {
// Code that executes if the condition is TRUE
}
else {
// Code that executes if the condition is FALSE
}
- if: Means “If…”.
- (): The question goes inside the parentheses.
- {}: The curly braces delimit what actions happen.
- else: (Optional) Means “If not…”. It’s what happens if the condition is not met.
Comparison Operators
For the if to work, we need to ask questions whose answer is True or False. For this we use comparison operators.
| Operator | Meaning | Example |
|---|---|---|
== | Is equal to? | if (x == 5) |
!= | Is not equal to? | if (x != 0) |
< | Less than | if (x < 10) |
> | Greater than | if (x > 10) |
In C++, the single equals sign = is used for assignment (putting a value into a box). To compare (ask if two things are equal), you must use the double equals ==.
a = 5“Store the 5 in variable a”.if (a == 5)“Does ‘a’ equal 5?”.
Practical Example: The Smart Button
Let’s revisit the setup from the previous article: a button connected to Pin 2 and the built-in LED on Pin 13.
We want:
- If I press the button, the LED turns on.
- If not, it turns off.
The code would look something like this:
void setup() {
pinMode(13, OUTPUT);
pinMode(2, INPUT_PULLUP); // we'll see this in due time
}
void loop() {
// 1. Read the button
int state = digitalRead(2);
// 2. Make the decision
if (state == LOW) {
// If the condition is true (button is pressed)
digitalWrite(13, HIGH); // Turn LED ON
}
else {
// If the condition is false (button is not pressed)
digitalWrite(13, LOW); // Turn LED OFF
}
}
When you upload this code, you’ll see that the behavior is identical to the previous tutorial, but the code is much more readable. We are clearly expressing our intention: “If A happens, do B”.
To understand this program “really well”, we would need to talk about how to read a switch and what a PULL_UP is. We’ll see that in due time, for now don’t worry too much about it.
What if I want it to work the other way around?
Imagine you want a “dead man’s switch” light (turns off if you press, turns on if you release). You just need to change the logic inside the curly braces, or change the question:
if (state == HIGH) { // If the button is RELEASED
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
Nesting Decisions
What if we want more complex conditions? We can put an if inside another if.
If I press the button… and also the LED was off… then turn it on.
This would allow us to make a toggle-type switch (a push button that turns on and off alternately), although that requires managing state variables which we’ll see later.
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// First question: Is the button pressed?
if (digitalRead(buttonPin) == HIGH) {
// If we get here, the button IS pressed.
// Now we ask the second question (nested):
if (digitalRead(ledPin) == LOW) {
// If we get here, BOTH conditions are met:
// 1. Button pressed
// 2. The LED was off
digitalWrite(ledPin, HIGH); // Turn the LED ON!
}
}
delay(100); // A small pause for stability
}
If this is too complicated for now, don’t worry. For now, just keep in mind that inside the curly braces { ... } you can write any valid code: turn LEDs on, wait with delay, or ask another if question.
