Control statements in Arduino are used to control the flow of execution in a program.
They allow you to make decisions, repeat actions, and manage program logic based on certain conditions or loops.
This tutorial covers the main control statements in Arduino, with examples to illustrate their usage.
Table of Contents
1. What Are Control Statements?
Control statements manage the execution flow of a program. They allow you to:
- Make decisions based on conditions.
- Repeat a block of code multiple times.
- Break or skip iterations in loops.
2. Types of Control Statements
- Conditional Statements: Execute code blocks based on specific conditions (if, else, switch).
- Looping Statements: Repeat code blocks until a condition is met (for, while, do-while).
3. Conditional Statements
3.1 if and else
The if statement executes a block of code if the condition is true. The else block executes if the condition is false.
Syntax
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
Example: Turn LED On Based on Button Press
const int buttonPin = 2; const int ledPin = 13; void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); // Turn LED on Serial.println("LED ON"); } else { digitalWrite(ledPin, LOW); // Turn LED off Serial.println("LED OFF"); } }
3.2 switch Case
The switch statement selects one of many code blocks to execute based on a variable’s value.
Syntax
switch (variable) { case value1: // Code to execute for value1 break; case value2: // Code to execute for value2 break; default: // Code to execute if no case matches break; }
Example: Control LED Brightness Using a Switch
const int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { int mode = 2; // Change mode for different brightness levels switch (mode) { case 1: analogWrite(ledPin, 64); // Low brightness break; case 2: analogWrite(ledPin, 128); // Medium brightness break; case 3: analogWrite(ledPin, 255); // High brightness break; default: analogWrite(ledPin, 0); // LED off break; } delay(1000); }
4. Looping Statements
4.1 for Loop
The for loop is used to repeat a block of code a specific number of times.
Syntax
for (initialization; condition; increment) { // Code to execute }
Example: Blink LED 10 Times
const int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); } void loop() { for (int i = 0; i < 10; i++) { digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); } while (true); // Stop further execution }
4.2 while Loop
The while loop executes a block of code as long as the condition is true.
Syntax
while (condition) { // Code to execute }
Example: Blink LED While Button Is Pressed
const int buttonPin = 2; const int ledPin = 13; void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { while (digitalRead(buttonPin) == HIGH) { digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(500); } }
4.3 do-while Loop
The do-while loop executes the code block at least once before checking the condition.
Syntax
do { // Code to execute } while (condition);
Example: Print Numbers Until 10
void setup() { Serial.begin(9600); int count = 1; do { Serial.println(count); count++; } while (count <= 10); } void loop() { // Empty }
5. Break and Continue
5.1 Break
- Terminates the current loop or switch statement.
Example: Stop Loop After 5 Iterations
void setup() { Serial.begin(9600); for (int i = 1; i <= 10; i++) { if (i > 5) { break; } Serial.println(i); } } void loop() { // Empty }
5.2 Continue
- Skips the rest of the current iteration and moves to the next iteration.
Example: Skip Even Numbers
void setup() { Serial.begin(9600); for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; } Serial.println(i); } } void loop() { // Empty }
6. Practical Examples
Example 6.1: LED Chase Sequence
const int ledPins[] = {2, 3, 4, 5}; const int numLeds = 4; void setup() { for (int i = 0; i < numLeds; i++) { pinMode(ledPins[i], OUTPUT); } } void loop() { for (int i = 0; i < numLeds; i++) { digitalWrite(ledPins[i], HIGH); delay(200); digitalWrite(ledPins[i], LOW); } }
Example 6.2: Temperature Alert System
const int tempPin = A0; void setup() { Serial.begin(9600); } void loop() { int temp = analogRead(tempPin); if (temp > 800) { Serial.println("High Temperature Alert!"); } else if (temp > 600) { Serial.println("Moderate Temperature."); } else { Serial.println("Temperature is Normal."); } delay(1000); }
7. Best Practices for Control Statements
- Use Indentation:
- Properly indent code for better readability.
- Avoid Infinite Loops:
- Ensure loop termination conditions are met.
- Minimize Nested Loops:
- Too many nested loops can make the program difficult to read and debug.
- Use Switch for Multiple Conditions:
- Use switch when handling multiple cases for a single variable.
- Optimize Conditions:
- Avoid redundant checks in conditional statements.
Conclusion
Control statements are fundamental for managing the flow of execution in Arduino programs. By using conditional and looping statements effectively, you can build powerful and dynamic Arduino applications.
This tutorial covered the essential control statements and provided examples to help you get started.
For more details, visit the official Arduino reference.