Conditional Control Structures
in Arduino IDE (C++)
G10 - DETERMINATION
Logical and Relational Operators
• They are used in programming to
make decisions based on
conditions.
• In Arduino (C++), they help
control the flow of the program
by evaluating expressions that
return either true or false.
Relational Operators
Relational Operators
(Comparison):
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Logical and Relational Operators
Overview
Operator Meaning Example Result
true if x
== Equal to x == y
equals y
true if x is
!= Not equal to x != y not equal to
y
true if x is
< Less than x < y
less than y
true if x is
> Greater than x > y greater than
y
true if x is
Less than or
<= x <= y less than or
equal to
equal to y
true if x is
Greater than
>= x >= y greater than
or equal to
or equal to y
Logical Operators
Operator Meaning Example Result
true if both
(x > 5 && y <
&& Logical AND conditions
10)
are true
|| Logical OR (X==4 || a<b) Logical OR
true if x is
! Logical NOT !(x == y) not equal to
y
EXAMPLE: Evaluate each expression
whether it is TRUE or FALSE
1. 10 == 5
2. !(7 >= 7)
3. 8 < 6
4. 23 > 24
5. (3 >= (3+4))
6. 4 <= 4 && 6 > 5
7. 5 > 3 && 2 < 4
8. !(3==3) || (6<=5)
9. !((3>4) || (23<=2) && (23 != 4))
10.((3 + 2) == 5 && ((6 / 2) == 3))
Introduction to Control Structures
• Control structures in Arduino
C++ allow you to control the
flow of your program. They
determine which instructions
are executed under certain
conditions.
Types of Conditional Control
Structures
1. if statement
2. if-else statement
3. if-else-if statement
4. switch-case statement
if Statement
Syntax:
if (condition) {
// action to execute if condition is
true
}
Example:
if (digitalRead(2) == HIGH) {
digitalWrite(13, HIGH);
}
Scenario 1: Turn on a LED if the temperature is
greater than 30°C otherwise, turn LED OFF if
temperature is less than 30.
int temperature = 29; // simulated value
int LED = 13;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
if (temperature > 30) {
digitalWrite(LED, HIGH); // Turn LED ON
Scenario 2: Turn on a LED connected to I/O pin 5
if a button switch (ACTIVE HIGH, I/O 6) is pressed.
int SW = 6; // switch pin
int LED = 5; //LED pin
int SWState;
void setup() {
pinMode(LED, OUTPUT);
pinMode(SW, INPUT);
void loop() {
SWState = digitalRead(SW);
if (SWState == HIGH) {
digitalWrite(LED, HIGH); // Turn LED ON
}
digitalRead() Function
This function is used to read
the state (HIGH or LOW) of a
digital input pin.
Function Syntax:
int value = digitalRead(pin);
if-else Statement
Syntax:
if (condition) {
// action if condition is true
} else {
// action if condition is false
}
Example:
if (temperature > 30) {
turnOnFan();
} else {
turnOffFan();
}
Scenario 1: Turn on the fan(connected to I/O if
the room is hot (temp is > 30), otherwise turn it off.
int temperature = 50;
void setup() {
pinMode(8, OUTPUT); // Fan control
}
void loop() {
if (temperature > 30) {
digitalWrite(8, HIGH); // Turn fan ON
} else {
digitalWrite(8, LOW); // Turn fan OFF
}
}
Scenario 2: Turn ON a
LED connected to I/O pin 5
if a button switch (ACTIVE
HIGH, I/O 6) is pressed,
and turns OFF if the switch
is NOT pressed.
Problem 2 – Dual LED Controller (using if-else)
•Components: 1 tact switch, 2 LEDs (red +
green)
•Task:
•If the button is pressed, turn ON the BLUE
LED and turn OFF the YELLOW.
•If the button is released, turn ON the
YELLOW LED and turn OFF the BLUE.
•Hint: Use if-else to swap LED states.
•Challenge Add-on: Make the red LED blink
when pressed instead of staying solid.
Problem 3 – Multi-Mode LED Controller (using if-else-
if)
•Components: 1 tact switch, 3 LEDs
•Task:
Each button press cycles through:
[Link] 1 → turn ON YELLOW LED
[Link] 2 → turn ON RED LED
[Link] 3 → turn ON BLUE LED
•Hint: Use a press counter and if-else-if to handle the
modes.
•Challenge Add-on: Reset back to Mode 1 after Mode
3.
int lightLevel = 600; // simulated analog value
void setup() {
pinMode(2, OUTPUT); // RED
pinMode(3, OUTPUT); // YELLOW
pinMode(4, OUTPUT); // GREEN
}
void loop() {
if (lightLevel < 300) {
digitalWrite(2, HIGH); // RED
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (lightLevel < 700) {
digitalWrite(2, LOW);
digitalWrite(3, HIGH); // YELLOW
digitalWrite(4, LOW);
} else {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH); // GREEN
switch-case Statement
Syntax:
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Efficient for evaluating multiple discrete values.
int input = 2;
void setup() {
[Link](9600); // Start serial communication
}
void loop() {
switch (input) {
case 1:
[Link](“The number is ONE.");
break;
case 2:
[Link](" The number is TWO.");
break;
case 3:
[Link](" The number is THREE.");
break;
default:
[Link]("Invalid input.");
break;
}
delay(500); // Small delay to prevent spamming
}
Scenario:
Show LED color based on value:
10 Red
20 Yellow
30 Blue
ALL LEDS are OFF if NONE of
the value is correct.
NOTE: RED(I/O 5),YELLOW(I/O
6),BLUE(I/O 7)
Scenario:
Show LED color based on a switch
press:
10 Red
20 Yellow
30 Blue
The LED will recycle back to the
first LED(RED) in the 4th press.
NOTE: RED(I/O 5),YELLOW(I/O
6),BLUE(I/O 7)
Scenario:
Write a code that turns a DC
motor when a button switch is
pressed and reverses rotatiuon
once the button is not pressed.
NOTE: connect the motor
terminals to poins 7 and 8,
Button to pin 9.
PROBLEM 1:
You are monitoring temperature using a sensor
(use a variable for simulation).
Write a program that reads the temperature and
displays a message based on its value:
– If temp ≥ 40 → “Warning: Overheating!”
– If temp ≥ 30 and < 40 → “Normal Operation”
– If temp < 30 → “Temperature Low – Standby
Mode”
Best Practices
✔ Use comments to explain your
logic.
✔ Keep conditions simple and
readable.
✔ Avoid deep nesting when
possible.
✔ Use switch-case for multiple
discrete values.
Conclusion
• Conditional control structures
are essential for decision-
making in Arduino programs.
• Mastering them allows students
to build more intelligent and
responsive systems.