Session 6: Navigating with While Loops and Simultaneous Tasks
Objective:
In this session, students will be introduced to the while loop and learn to use it to create a continuously running condition-based routine. They’ll apply this by coding their robot to follow a curved line while simultaneously detecting and navigating around obstacles. The robot will use both the line sensor (on the back) and distance sensor (in front), adjusting its movement to stay on track. Additionally, students will program the robot to send notifications via the remote BLE client whenever it detects an obstacle, reinforcing multitasking and real-time communication.
1. Introduction to While Loops (10 minutes)
Activity:
Begin with an engaging discussion on how actions can be repeated based on ongoing conditions. Introduce the concept of a while loop as a way to repeat instructions as long as a certain condition remains true.
Discussion Examples:
You may say:
“Imagine you’re walking along a path, and you want to keep going until you reach a wall. You don’t know how many steps you’ll take, so you keep repeating ‘one step forward’ until you hit the wall. This is like a while loop—repeating actions based on a condition, like ‘no wall detected.’”
Then, introduce the syntax of a while loop and explain how the loop runs continuously until the condition is no longer met.
Example Code:
#include <M3DGo.h>
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
while (range.get_cm() > 15) { // While there's no obstacle within 15 cm
go.forward(50); // Move forward at 50% speed
}
go.stop(); // Stop when an obstacle is detected
}
Teacher’s Role:
Demonstrate this simple code on the projector, explaining how the robot moves forward until the condition (distance sensor reading > 15 cm) is no longer true. This prepares students for using the while loop to track continuous conditions.
2. Connecting the Line Sensor and Distance Sensor (10 minutes)
Activity:
Introduce the concept of combining multiple sensors to perform simultaneous tasks. In this activity, the robot will use the line sensor on the back and the distance sensor on the front to follow a curved line while detecting obstacles.
Instructions:
- Guide students to connect both sensors properly.
- Introduce the following commands:
line.getRight()andline.getLeft()to sense the line position.range.get_cm()to detect objects within a specified distance.
Teacher’s Role:
Ensure students understand how each sensor will contribute to the task. Explain that the robot will use the line sensor to detect its position on the line and the distance sensor to detect objects in front.
3. Programming the Robot for Line Following with Obstacle Detection (25 minutes)
Activity:
Now, students will write code to make the robot follow the line using while loops while checking for obstacles. The robot should adjust if it starts veering off the line and navigate around obstacles if detected.
Example Code:
#include <M3DGo.h>
void setup() {
go.begin();
}
void loop() {
while (line.onTheLine()) { // Infinite loop to keep the robot continuously checking
if (range.get_cm() < 15) { // Obstacle detected within 15 cm
remote.notify("Obstacle Detected!"); // Notify via remote BLE
go.stop();
go.delay(1000); // Pause for effect
// Obstacle avoidance routine
go.spinClockwise(90); // Turn right
go.stop();
go.forward(50); // Move forward to clear obstacle
go.delay(1000);
go.stop();
go.spinCounterClockwise(90); // Turn left to realign with the line
go.forward(); // Move forward to clear obstacle
go.delay(1000);
go.spinCounterClockwise(90);
go.forward(50);
go.delay(1000);
go.spinClockwise(90);
go.stop();
}
// Curved line Instruction
if (line.getLeft() < line.getRight()) { // Robot is veering off to the right
go.spinCounterClockwise(30); // Adjust to the left
} else if (line.getRight() < line.getLeft()) { // Robot is veering off to the left
go.spinClockwise(30); // Adjust to the right
} else {
go.forward(50); // Move forward if centered on line
}
go.delay(100); // Small delay for stability
}
}
Explanation:
- Obstacle Detection: If the distance sensor detects an obstacle, the robot stops, broadcasts a message, and performs a right turn, moves forward to bypass the obstacle, then returns to the line.
- Line Following: The robot adjusts its direction based on the readings from the left and right line sensors to stay centered on the line.
Teacher’s Role:
Guide students through the code, explaining each section, especially the dual use of sensors and the role of the while loop in continuous tracking. Encourage them to experiment by modifying the distance threshold or line-following adjustments.
4. Hands-On Testing and Debugging (10 minutes)
Activity:
Allow students to upload their code and test the robot’s behavior on a curved line path with obstacles. They can observe how the robot navigates around objects while staying on the line.
Instructions:
- Encourage students to test different obstacle positions to see how the robot adapts.
- Remind them to observe the robot’s notifications on the remote BLE when it detects an obstacle.
Teacher’s Role:
Assist students in debugging, emphasizing how each sensor influences the robot’s movements. Encourage them to make adjustments if the robot isn’t following the line smoothly or stopping effectively for obstacles.
5. Reflection and Wrap-Up (5 minutes)
Reflection Questions:
You may ask:
- “How did the while loop help in checking for obstacles continuously?”
- “What adjustments did you make to improve line following or obstacle detection?”
- “How did using both sensors change the robot’s behavior compared to using just one?”
Teacher’s Role:
Guide the discussion, reinforcing the importance of while loops for ongoing checks and the power of combining sensor data for more complex decision-making.
Learning Outcome:
By the end of this session, students will understand how to use while loops to enable continuous condition-based actions and apply dual-sensor feedback for line following and obstacle avoidance in real-time.