Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

🔁 Loops (for / while)

🎯 Lesson Objective

In this lesson, students will understand:

• What loops are in programming
• Why loops are used in robotics programming
• How the for loop works
• How the while loop works
• How loops help repeat tasks automatically

This lesson introduces loops, which allow programs to repeat instructions multiple times without rewriting the same code.


1️⃣ What is a Loop?

A loop is a programming structure that allows a set of instructions to be executed repeatedly.

Instead of writing the same code many times, loops allow the program to repeat a task automatically.

Loops are commonly used when a task must be repeated multiple times.

For example:

• Blinking an LED several times
• Reading sensor data repeatedly
• Moving a robot forward for a certain number of steps
• Repeating calculations in a program

Loops make programs shorter, more efficient, and easier to manage.


2️⃣ Why Loops Are Important in Robotics

In robotics systems, many operations need to happen repeatedly.

Examples include:

🔎 Continuously reading sensor data
⚙️ Repeatedly checking robot conditions
📡 Sending data to the serial monitor
🤖 Monitoring the robot’s environment

Although the loop() function already repeats continuously, loops inside programs allow more controlled repetition.

For example:

A robot might blink an LED five times when a task is completed.

This can be done using a loop.


3️⃣ The for Loop

The for loop is used when the number of repetitions is known.

It repeats a block of code a specific number of times.

Basic Syntax

 
for (initialization; condition; increment) {
// code to repeat
}

The three parts of a for loop are:

Initialization → Starting value
Condition → How long the loop continues
Increment → How the counter changes each time


4️⃣ Example of a for Loop

Example program to blink an LED five times:

 
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}

Explanation:

• The variable i starts at 0
• The loop continues until i < 5
• After each repetition, i increases by 1

This causes the LED to blink five times.


5️⃣ The while Loop

The while loop is used when the number of repetitions is not known beforehand.

The loop continues running as long as a condition remains true.

Basic Syntax

 
while (condition) {
// code to repeat
}

If the condition becomes false, the loop stops.


6️⃣ Example of a while Loop

Example:

 
while (distance < 20) {
stopRobot();
}

Explanation:

• The robot checks if the distance is less than 20 cm.
• If the condition remains true, the robot continues stopping.
• When the obstacle moves away, the loop stops.

This allows the robot to respond dynamically to environmental conditions.


7️⃣ Differences Between for and while Loops

Feature for Loop while Loop
Repetition count Known Unknown
Usage Fixed number of repetitions Condition-based repetition
Common use Counters and repeated tasks Sensor-based conditions

Both loops are useful depending on the situation.


8️⃣ Loops in Robotics Projects

Loops are used in many robotics applications.

Examples include:

🤖 Repeating sensor checks
📡 Sending continuous data
💡 Blinking status LEDs
⚙️ Running motors for a specific time

Loops allow programs to perform repetitive tasks efficiently and automatically.


🚀 What Happens Next

Now that you understand how loops allow programs to repeat tasks automatically, the next step is to learn how to organize programs into reusable blocks of code.

Scroll to Top