Session 7: Introduction to Loops
Session Architect: Muhammad Bilal
Duration: 50-60 minutes
Equipment Required:
- M3D Robotics kit
- Laptops with Arduino IDE pre-installed
- USB/OTG cables to connect the robot to the laptop
- Paper and pencils for students to document their discoveries
Learning Objectives:
- Understand the concept of loops and how they allow repetitive tasks in code.
- Learn how to use for loops in Arduino C++ to control how many times an action is repeated.
- Incorporate previous knowledge of robot movement and sensors into the use of loops.
- Write code using loops to control the robot’s movement and sensor input.
1. Introduction to the Concept of Loops (10 minutes)
Activity:
Start with an interactive activity to demonstrate repetition and how loops allow us to repeat actions in coding.
Instructions:
- Set the Stage:
You may ask the students:
“What do you do when you need to repeat something multiple times, like counting or skipping rope?”
They might say:
“I just keep doing it until I’m done!”
Connect this to loops:
“Exactly! We often repeat actions in real life, like brushing our teeth for a certain number of minutes or walking a certain number of steps. In coding, we use something called a ‘loop’ to repeat actions a set number of times.”
- Physical Example:
Ask for volunteers to come up and perform a task multiple times, such as clapping five times.
“Let’s say I want you to clap your hands five times. You wouldn’t write out the word ‘clap’ five times, would you? Instead, you could say, ‘clap five times.’ This is what a loop does for us in coding—it repeats something without us having to write it out each time.”
- Connect to Robot Coding:
Relate this to what the students have done with the robot so far:
“Up until now, you’ve used the void loop() function to keep repeating actions, but what if you wanted the robot to move forward five times and then stop? A loop can help us control exactly how many times something happens.”
- You may relate for loop with if-then-else commands that they have studied previously. If acts on instructions based on some condition once while for acts on instructions as long as the condition is true.
Teacher’s Role:
- Lead the discussion and physical activity to help students understand repetition.
- Make sure students understand that loops are used when we want to repeat a task a specific number of times.
2. Introducing the For Loop (10 minutes)
Activity:
Introduce the for loop and explain how it works step by step.
Instructions:
- Basic Explanation of For Loops:
Write a basic for loop structure on the board:
for (int i = 0; i < 5; i++) {
// Actions to repeat
}
Break down each part of the for loop:
- int i = 0;: This starts the loop counter.
- i < 5;: This sets the condition for how many times the loop will run.
- i++: This increases the counter by 1 after each loop. It helps the robot keep track of how many times the action has been repeated and it increases the count by 1 each time the robot performs the specified action.
“This code means we’re going to repeat an action 5 times. Each time the loop runs, the value of
iincreases by 1. Onceiis no longer less than 5, the loop stops.”
- Simple Example with Robot Movement:
Show how this works with robot movement. Write a simple program that moves the robot forward five times:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
for (int i = 0; i < 5; i++) {
go.forward(50); // Move forward at 50% speed
delay(1000); // Wait 1 second
go.stop(); // Stop briefly between moves
delay(500); // Wait half a second
}
}
- Explain that each loop makes the robot move forward for 1 second, then stop for 0.5 seconds.
Teacher’s Role:
- Guide the students through each part of the loop, ensuring they understand how the loop repeats.
- Help them understand how the loop control variables (e.g.,
i) allow for a specific number of repetitions.
3. Experimenting with For Loops (20 minutes)
Challenge 1: Move Forward and Backward with Display Feedback
Objective:
Students will modify the code to make the robot move forward for 1 second, then backward for 1 second, and repeat this 5 times. Each time it moves, the display will show whether the robot is moving forward or backward.
Example Code:
void loop() {
for (int i = 0; i < 5; i++) {
display.text("Moving Forward!"); // Display message on screen
go.forward(50); // Move forward at 50% speed
delay(1000); // Wait 1 second
display.text("Moving Backward!"); // Display message on screen
go.reverse(50); // Move backward at 50% speed
delay(1000); // Wait 1 second
}
}
Challenge 2: Add a Spin with Display Feedback
Objective:
Modify the code to make the robot spin 90 degrees between each forward and backward movement, while displaying the action being performed on the screen.
Example Code:
void loop() {
for (int i = 0; i < 5; i++) {
display.text("Moving Forward!");
go.forward(50); // Move forward at 50% speed
delay(1000); // Wait 1 second
display.text("Moving Backward!");
go.reverse(50); // Move backward at 50% speed
delay(1000); // Wait 1 second
display.text("Spinning!"); // Display spinning message
go.spinClockwise(90); // Spin 90 degrees clockwise
delay(500); // Wait half a second between spins
}
}
Challenge 3: Sensor Input with Display Feedback
Objective:
Use a for loop to check the distance sensor 10 times. If the sensor detects an obstacle, the robot will stop and display “Obstacle Detected!”. Otherwise, it will move forward and display “Clear Path!”.
Example Code:
void loop() {
for (int i = 0; i < 10; i++) {
if (range.get_cm() > 20) { // If no obstacle is detected
display.text("Clear Path!"); // Display message
go.forward(50); // Move forward
} else {
display.text("Obstacle Detected!"); // Display message
go.stop(); // Stop if obstacle is detected
}
delay(500); // Wait 0.5 seconds before checking again
}
}
Challenge 4: Speed Control Based on Loop Iteration
Objective:
Adjust the robot’s speed based on the current loop iteration. For example, start at 20% speed and increase by 20% each time it moves forward, while displaying the current speed on the screen.
Example Code:
void loop() {
for (int i = 1; i <= 5; i++) { // Loop runs 5 times
int speed = i * 20; // Increase speed by 20% each iteration
display.text("Speed: " + String(speed) + "%"); // Show current speed on the screen
go.forward(speed); // Move forward at the calculated speed
delay(1000); // Wait for 1 second
go.stop();
delay(500); // Pause for half a second before the next loop
}
}
Challenge 5: Line Sensor and Display Integration
Objective:
Program the robot to follow a line and display whether it is on the line or off the line. Each time the robot detects the line, it will display “On the Line!” and move forward. If it’s not on the line, it will display “Off the Line!” and stop.
Example Code:
void loop() {
for (int i = 0; i < 10; i++) {
if (go.onTheLine()) { // Check if the robot is on the line
display.text("On the Line!"); // Display message
go.forward(50); // Move forward if on the line
} else {
display.text("Off the Line!"); // Display message
go.stop(); // Stop if off the line
}
delay(500); // Wait before checking again
}
}
4. Reflection and Wrap-Up (5 minutes)
After completing these challenges, ask students to reflect on the following questions:
- “How did using loops help simplify your code?”
- “What did you notice when combining loops with sensors and display messages?”
- “Can you think of other examples where loops could make coding easier?”
Encourage students to share their insights about using loops and display feedback to make their code more interactive and responsive.
Learning Outcome:
By the end of this session, students will have a clear understanding of how to use for loops to repeat actions in a controlled way, integrate sensor input, and display messages to create more dynamic and interactive robot behaviors.