Arduino Loops 101

Besides setup() and loop(), Arduino has few other useful and easy to use loops. This is small collection of short and basic introductory topics related to Arduino loops and loops in programming in general. These are not extensive courses, and google will give you plenty in-depth results and examples.

For loops

For loop is a control structure used for repetitive tasks. It allows you to execute a block of code a specific number of times. The basic syntax of a for loop in Arduino is as follows:

for (initialization; condition; increment/decrement) {
  // Code to be executed
}

Let's break down each part of the for loop:

initialization:
This part is used to initialize a loop control variable. It typically declares and assigns an initial value to a variable that will be used to control the loop. For example, you might declare and initialize a variable like int i = 0;.

condition:
This is a Boolean expression that is evaluated before each iteration of the loop. As long as the condition is true, the loop will continue to execute. When the condition becomes false, the loop will exit. For example, you can use i < 10 as a condition to loop 10 times.

increment/decrement:
This part is used to modify the loop control variable after each iteration. It can be an increment (e.g., i++ to increase the value of i by 1) or a decrement (e.g., i-- to decrease the value of i by 1).

Here's a simple example of a for loop that blinks an LED 10 times:

int ledPin = 13; // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT); // Initialize the LED pin as an output
}

void loop() {
  for (int i = 0; i < 10; i++) {
    digitalWrite(ledPin, HIGH); // Turn the LED on
    delay(1000);               // Wait for 1 second
    digitalWrite(ledPin, LOW);  // Turn the LED off
    delay(1000);               // Wait for 1 second
  }
}

In this example, the for loop runs 10 times, with i ranging from 0 to 9. During each iteration, it turns the LED on, waits for 1 second, turns the LED off, and waits for another second. You can use for loops to iterate through arrays, perform calculations, and repeat tasks as needed in your Arduino projects. Just make sure to set up the initialization, condition, and increment/decrement parts appropriately for your specific application.


While loops

A while loop is another fundamental control structure in programming, including in Arduino programming. It allows you to repeatedly execute a block of code as long as a specified condition is true. Here's a basic overview of while loops in the context of Arduino:

while (condition) {
  // Code to be executed while the condition is true
}

Let's break down while loop:

Condition:
The condition is a boolean expression (true or false) that is evaluated before every iteration of the loop.

If the condition is true, the code inside the while loop will be executed. Once the code inside the loop is executed, the condition is re-evaluated.

If the condition is still true, the loop continues to execute, and this process repeats until the condition becomes false. When the condition is false, the loop terminates, and the program continues with the code after the while loop.

Example:

int count = 0;

while (count < 5) {
  Serial.print("Count: ");
  Serial.println(count);
  count++;  // Increment count by 1
  delay(1000);  // Delay for 1 second
}

Serial.println("Loop finished.");

In this example, the while loop will continue to execute as long as the count variable is less than 5. Inside the loop, it prints the value of count, increments count by 1, and then adds a 1-second delay. The loop repeats this process until count reaches 5, at which point the loop terminates, and "Loop finished" is printed.

Be cautious when using while loops in programming. If the condition never becomes false, it can result in an infinite loop, which may cause your program to become unresponsive. Always make sure the condition is designed to eventually become false or include a mechanism to exit the loop when needed (e.g., using a break statement).


Do-While loops

A "do-while" loop in Arduino, like in many other programming languages, is a control structure that allows you to repeatedly execute a block of code as long as a certain condition is true. It's called a "do-while" loop because it first does the execution of the code inside the loop and then checks the condition to determine whether to continue looping.
Here's a basic overview of how to use a "do-while" loop in Arduino:

do {
  // Code to be executed at least once
} while (condition);

Here's what each part of the "do-while" loop does:

do:

This keyword marks the beginning of the loop, and it tells the Arduino to execute the code block inside the loop at least once, regardless of the condition.

{ /* Code to be executed */ }:

This is the block of code that you want to repeat. You can place any valid Arduino code inside this block.

while (condition):

This part of the loop specifies the condition that is checked after the code block is executed. If the condition is true, the loop will continue to execute, and if it's false, the loop will exit, and the program will continue with the code after the loop.

Here's an example of how you might use a "do-while" loop in an Arduino sketch:

int count = 0;

do {
  Serial.println("This is iteration " + String(count));
  count++;
} while (count < 5);

In this example, the code inside the "do" block will execute at least once, printing a message to the serial monitor. After each iteration, the count variable is incremented, and the loop will continue as long as count is less than 5.

The "do-while" loop is useful when you want to ensure that a specific block of code is executed at least once, even if the condition is initially false. It's commonly used for input validation and other situations where you need to ensure a certain action takes place before checking the condition.


← Back