Arduino - Control Statements
In Arduino programming, as in C/C++, control statements are used to control the flow of execution in your sketch. These statements allow you to make decisions, repeat blocks of code, and alter the flow based on conditions. Here's an overview of the control statements available in Arduino:
1. Conditional Statements
if
Statement
The if
statement executes a block of code if a specified condition is true.
cppint sensorValue = analogRead(A0);
if (sensorValue > 500) {
digitalWrite(LED_PIN, HIGH); // Turn LED on
}
if-else
Statement
The if-else
statement executes one block of code if the condition is true, and another block if it
is false.
cppint sensorValue = analogRead(A0);
if (sensorValue > 500) {
digitalWrite(LED_PIN, HIGH); // Turn LED on
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off
}
else-if
Statement
The else-if
statement allows you to check multiple conditions sequentially.
cppint sensorValue = analogRead(A0);
if (sensorValue > 700) {
// Do something if sensor value is greater than 700
} else if (sensorValue > 500) {
// Do something else if sensor value is greater than 500
} else {
// Do something if neither condition is true
}
2. Loop Statements
for
Loop
The for
loop executes a block of code a specified number of times.
cppfor (int i = 0; i < 10; i++) {
// Code to repeat 10 times
}
while
Loop
The while
loop executes a block of code as long as a specified condition is true.
cppint sensorValue = analogRead(A0);
while (sensorValue < 100) {
// Code to execute while condition is true
sensorValue = analogRead(A0); // Update sensor value
}
do-while
Loop
The do-while
loop is similar to the while
loop, but it executes the block of code at
least once before checking the condition.
cppint sensorValue;
do {
sensorValue = analogRead(A0);
// Code to execute at least once
} while (sensorValue < 100);
3. Control Flow Statements
switch-case
Statement
The switch-case
statement checks the value of a variable against multiple cases and executes the
corresponding block of code.
cppint option = 2;
switch (option) {
case 1:
// Code for case 1
break;
case 2:
// Code for case 2
break;
default:
// Code if no case matches
break;
}
break
Statement
The break
statement is used inside loops and switch-case
statements to terminate the
loop or switch statement immediately.
cppfor (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
}
continue
Statement
The continue
statement is used inside loops to skip the current iteration and proceed to the next
iteration.
cppfor (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
// Code here only executes for odd numbers
}
Example Sketch Using Control Statements
Here’s an example Arduino sketch demonstrating various control statements:
cppint sensorPin = A0;
int ledPin = 13;
int threshold = 500;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
// Conditional statement example
if (sensorValue > threshold) {
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(ledPin, LOW); // Turn LED off
}
// Loop statement example
for (int i = 0; i < 5; i++) {
Serial.print("Iteration: ");
Serial.println(i);
delay(500);
}
// Switch-case statement example
switch (sensorValue / 100) {
case 0:
Serial.println("Sensor value less than 100");
break;
case 1:
Serial.println("Sensor value between 100 and 199");
break;
case 2:
Serial.println("Sensor value between 200 and 299");
break;
default:
Serial.println("Sensor value 300 or more");
break;
}
delay(1000);
}
Tips for Using Control Statements
- Use Clear and Descriptive Variable Names: This makes your code easier to understand and maintain.
- Indent Code Properly: Proper indentation improves readability, especially when nesting control statements.
- Avoid Deep Nesting: Deeply nested control statements can make code difficult to follow. Consider refactoring into smaller functions if necessary.
- Test and Debug: Test your control statements thoroughly to ensure they behave as expected under different conditions.
Mastering control statements allows you to create more responsive and complex Arduino sketches, enabling you to control hardware based on various conditions and iterate over tasks efficiently.