Session 4: Advanced Line Following with Web BLE Integration
Duration: 60 minutes
Equipment Required:
- M3D Robotics kit with line sensor
- Laptops with Arduino IDE and Web BLE console setup
- USB/OTG cables to connect the robot to the laptop
- Projector for live coding demonstration
- Track designs with varying brightness levels and line widths (e.g., straight, curved paths)
Learning Objectives:
- Refine line-following algorithms to adapt to changes in brightness and line width.
- Use Web BLE commands to receive user inputs and send notifications.
- Utilize the line sensor and commands effectively to detect line position and adjust the bot’s movement.
- Implement logical operators, including the
notoperator, to create robust conditions in the code. - Add emojis to display the robot’s “feelings” for different line-following states.
1. Introduction to Web BLE Console and Commands (10 minutes)
Activity:
Begin with an engaging discussion about the concept of communication between a robot and a user. Ask students:
“How do you think robots communicate with us? Can they speak or use body language like humans, or might they need other ways to give us information?”
Guide them toward the idea of using digital interfaces like Web BLE. Explain that Web BLE is like a messaging tool for the robot, enabling it to send notifications, ask questions, and even receive user instructions to help it navigate or complete tasks.
Commands Overview:
remote.notify("Message")- Sends a simple notification message to the Web BLE console.remote.askNumber("Enter a number")- Prompts the user to enter a numeric value.remote.askString("Enter a string")- Prompts the user to enter a string.
Example Code for Notifications:
remote.notify("Starting line-following program...");
Teacher’s Role:
Walk students through a short demonstration of sending messages to Web BLE. Ask them to brainstorm other messages the robot might send and why it might be useful to receive updates as the robot performs tasks. Allow them to experiment by sending their own messages to Web BLE, sparking curiosity about how this “conversation” helps the robot adapt and respond.
2. Using the not Operator to Refine Conditions (5 minutes)
Activity:
Introduce the not operator (!) as a way to check for opposite conditions. Start by asking:
“What if we wanted the robot to react only when it’s NOT on the line? How would that change what it needs to do?”
Explain that the not operator allows us to look for when something is not true, such as if the robot is not on the line. Show how this can help create more specific conditions in programming.
Example Code:
if (!line.onTheLine()) { // If the robot is not on the line
display.text("Off the Line!"); // Show message on the robot's display
remote.notify("Robot is off the line"); // Show message on Web BLE
}
Teacher’s Role:
Guide students through the example and ask them to imagine scenarios where they would need to check for the opposite condition, like if something is absent or missing. Encourage them to think of real-life examples, such as “If you’re NOT at school, where might you be?”
3. Understanding the Line Sensor Commands and Functionality (10 minutes)
Activity:
Introduce the line sensor and explain how it has two IR sensors, one on the left and one on the right. Begin by asking:
“How do you think the robot can tell if it’s on a line or veering off? What could it be measuring to know where it is?”
Guide them to understand that the line sensor detects changes in brightness to identify if the robot is on or off the line.
Commands to Introduce:
line.getLeft()andline.getRight()- Retrieve values from the left and right sensors.line.onTheLine()andline.notOnLine()- Check if the robot is on or off the line.line.leftOfLine()andline.rightOfLine()- Detect if the robot is left or right of the line.
Example Activity:
Guide students to use line.getLeft() and line.getRight() to see how values change based on brightness and line width. Show them how both sensors’ values align when the robot is centered and differ when it veers off the line.
- Encourage them to put the sensor on different surfaces and different colors to see how the value changes.
Teacher’s Role:
Lead a brief discussion, asking questions like:
“What do you think would happen if one sensor reads a higher value than the other? How might that tell us which direction the robot should turn?”
Encourage students to test how the robot adjusts when it’s off-center, fostering curiosity about how sensors guide the robot along different types of paths.
4. Coding the Robot to Follow a Straight Line (10 minutes)
Activity:
Guide students in coding the robot to follow a straight line, using line sensor readings to adjust its path. Ask students:
“What happens if the robot starts to veer off the line? How could we use the sensor readings to keep it centered?”
Example Code for Straight Line Following:
#include <M3DGo.h>
void setup() {
go.begin();
remote.notify("Starting line-following task");
}
void loop() {
if (line.onTheLine()) {
go.forward(50); // Move forward
display.text("On the Line");
remote.notify("Following straight line");
} else if (line.leftOfLine()) {
go.spinClockwise(20); // Adjust right
display.emoji("Confused");
remote.notify("Adjusting right to get back on line");
} else if (line.rightOfLine()) {
go.spinCounterClockwise(20); // Adjust left
display.emoji("Confused");
remote.notify("Adjusting left to get back on line");
}
}
Teacher’s Role:
Guide students through each condition, asking questions to spark curiosity about how adjustments keep the robot on track. Encourage them to test different line widths and observe the robot’s corrections.
5. Coding for Curved Path Following (10 minutes)
Activity:
Extend the straight-line code for curved paths, adjusting based on sensor readings to keep the robot on track. Introduce a simple question:
“Do you think following a curve might require different adjustments than a straight line? Why?”
Explain how reading values from line.getLeft() and line.getRight() helps the robot navigate curved lines smoothly.
Example Code for Curved Path Following:
void loop() {
int leftSensor = line.getLeft();
int rightSensor = line.getRight();
if (leftSensor > rightSensor) { // Robot is left of the line
go.spinClockwise(20); // Adjust right
remote.notify("Curving right to stay on path");
display.emoji("Smile");
} else if (rightSensor > leftSensor) { // Robot is right of the line
go.spinCounterClockwise(20); // Adjust right
remote.notify("Curving right to stay on path");
display.emoji("Smile");
} else {
go.forward(50); // Move forward if on the line
display.emoji("Joy");
remote.notify("Following line smoothly");
}
}
Teacher’s Role:
Guide students to test and observe how the robot’s path varies with line curves. Ask them:
“How does the robot adjust on a curve compared to a straight line? Why might it need to make more frequent corrections?”
Encourage students to experiment with different curve angles and see how adjustments allow for smoother tracking.
6. Adding Web BLE Interactions for User-Directed Adjustments (10 minutes)
Activity:
Incorporate Web BLE to add interactive user input if the robot goes off the line entirely. Introduce with:
“What if the robot loses the line completely? Could a quick ‘help’ from us, like pressing a direction, get it back on track?”
Use Web BLE to ask the user for direction and help the robot readjust based on user feedback.
Example Code for User Input Adjustment:
if (!line.onTheLine()) { // Robot is off the line completely
go.stop();
display.text("Off the Line - Awaiting Input");
int direction = remote.askNumber("Enter direction (1: Left, 2: Right)");
if (direction == 1) {
go.spinCounterClockwise(20); // Turn left
display.emoji("Confused");
remote.notify("Adjusting left based on user input");
} else if (direction == 2) {
go.spinClockwise(20); // Turn right
display.emoji("Confused");
remote.notify("Adjusting right based on user input");
}
go.forward(50); // Resume following
}
Teacher’s Role:
Encourage students to observe how user input assists in navigation. Ask:
“Why might it be useful for the robot to ask for directions instead of just guessing?”
Discuss how real-life robots might need occasional guidance, especially in unpredictable environments.
7. Reflection and Wrap-Up (5 minutes)
Reflection Questions:
- “How did the robot behave on different line paths? Was it more challenging on curved lines?”
- “Why might adjusting to different brightness levels be important for robots in real-life scenarios?”
- “Where else might using the
notoperator help control robot behavior?”
Teacher’s Role: Wrap up by discussing how these refined line-following techniques make the robot more adaptive. Emphasize how user input, sensors, and logical conditions help create a responsive, versatile robot.
Learning Outcome:
By the end of this session, students will be able to program the robot to navigate both straight and curved lines, adjust to different brightness levels, and interact with users for guidance via Web BLE. They’ll understand how to combine sensors, conditions, and interactive inputs to create a flexible, responsive robot.