Arduino programming uses control structures such as if, for, while, and switch case to control decision-making and loops in a sketch. These structures allow the Arduino to respond to conditions, repeat tasks, and execute different blocks of code based on input.
1. If Statement (Conditional Execution)
The if statement is used to execute a block of code only if a specified condition is met.
Syntax
if (condition) {
// Code to execute if condition is true
}
Example: Turning an LED On Based on a Button Press
const int buttonPin = 2; // Button connected to pin 2
const int ledPin = 13; // LED connected to pin 13
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read button state
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn LED on if button is pressed
} else {
digitalWrite(ledPin, LOW); // Turn LED off otherwise
}
}
If-Else Statement
if (temperature > 30) {
Serial.println("It's too hot!");
} else {
Serial.println("Temperature is normal.");
}
If-Else If Statement
if (temperature > 30) {
Serial.println("It's too hot!");
} else if (temperature < 10) {
Serial.println("It's too cold!");
} else {
Serial.println("Temperature is comfortable.");
}
2. For Loop (Repeating Tasks a Fixed Number of Times)
A for loop runs a block of code a fixed number of times. It is commonly used for iterating over arrays or controlling repetitive tasks.
Syntax
for (initialization; condition; increment) {
// Code to execute in each iteration
}
Example: Blinking an LED 5 Times
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 5; i++) { // Loop runs 5 times
digitalWrite(ledPin, HIGH); // Turn LED on
delay(500); // Wait 500 ms
digitalWrite(ledPin, LOW); // Turn LED off
delay(500);
}
delay(2000); // Pause before repeating
}
Example: Running Through an Array
int numbers[] = {1, 2, 3, 4, 5};
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 5; i++) {
Serial.println(numbers[i]); // Print each number in the array
}
delay(2000);
}
3. While Loop (Repeating Until a Condition is Met)
A while loop runs a block of code as long as a specified condition remains true.
Syntax
while (condition) {
// Code to execute while the condition is true
}
Example: Waiting for a Button Press
const int buttonPin = 2;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("Waiting for button press...");
while (digitalRead(buttonPin) == LOW) {
// Stay in loop until button is pressed
}
Serial.println("Button pressed!");
}
Example: Countdown Timer
int count = 10;
void setup() {
Serial.begin(9600);
}
void loop() {
while (count > 0) {
Serial.print("Countdown: ");
Serial.println(count);
count--;
delay(1000);
}
Serial.println("Liftoff!");
delay(5000); // Restart countdown after delay
count = 10; // Reset count
}
4. Switch Case (Handling Multiple Conditions Efficiently)
A switch case statement is used when multiple conditions need to be checked, making it an alternative to if-else if-else chains.
Syntax
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
default:
// Code to execute if none of the cases match
}
Example: Controlling an LED with a Rotary Switch
const int ledPin = 13;
int mode = 1; // Example mode variable
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
switch (mode) {
case 1:
Serial.println("Mode 1: LED ON");
digitalWrite(ledPin, HIGH);
break;
case 2:
Serial.println("Mode 2: LED BLINKING");
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
break;
case 3:
Serial.println("Mode 3: LED OFF");
digitalWrite(ledPin, LOW);
break;
default:
Serial.println("Invalid Mode");
break;
}
}
Example: Using a Button to Cycle Through Modes
const int buttonPin = 2;
int mode = 1;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
mode++;
if (mode > 3) mode = 1; // Reset mode to 1 if it exceeds 3
delay(500); // Debounce delay
}
switch (mode) {
case 1:
Serial.println("Mode 1: Low Power Mode");
break;
case 2:
Serial.println("Mode 2: Normal Mode");
break;
case 3:
Serial.println("Mode 3: High Performance Mode");
break;
default:
Serial.println("Invalid Mode");
break;
}
}
Conclusion
- if statements allow conditional execution based on sensor readings or button presses.
- for loops are useful for repetitive tasks with a known count, such as blinking an LED.
- while loops execute code continuously until a specific condition is met.
- switch case statements simplify decision-making when handling multiple conditions efficiently.
These structures enhance Arduino programming by making it easier to manage loops, conditions, and device control. 🚀