1. Introduction to Conditionals in Arduino
What are Conditionals?
In programming, conditionals let your program make decisions. This means it can do different things based on different inputs. Think of it like: “If it’s raining, use an umbrella.”
In Arduino, conditionals help you respond to real-world inputs.
For example, “If the light level is low, turn on the LED.” Without conditionals, your Arduino sketch would not be interactive.
🧠 Example: Light Sensor Decision
Let's say you want to turn on an LED if it's dark:
// Read the light sensor value
int lightValue = analogRead(A0);
if (lightValue < 300) {
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
} else {
digitalWrite(LED_BUILTIN, LOW); // turn off LED
}
In the code above:
analogRead(A0)reads the value from the light sensor- If the light is below 300 (dark), the LED is turned on
- Otherwise, it's turned off
📹 Watch: Using Conditionals in Real Arduino Projects
✅ Practice Task:
Modify the code to make the LED blink when it’s dark, instead of staying on.