Session 2: Making Decisions with If-Else Statements
Duration: 50-60 minutes
Objective:
Introduce students to if-then-else conditionals to allow the bot to make decisions based on sensor input. They’ll create simple decision trees that help the bot choose its actions, combining movement, sensor data display, and responses to specific sensor readings.
Equipment Required:
- M3D Robotics kit
- Laptops with Arduino IDE
- USB/OTG cables
- Sensors (distance sensor)
- Paper and pencils for note-taking
Learning Objectives:
- Understand the basic structure and purpose of if-then-else statements in programming
- Write simple conditional statements to control the robot’s behavior
- Create decision trees that respond to specific inputs from the robot’s sensors
- Learn how to display actions and sensor data on the robot’s screen
1. Introduction to Conditionals and If-Then Logic (10 minutes)
Activity:
Begin the session with an engaging discussion about how decisions are made based on certain conditions in real life. This will lead into a simple introduction to if-then logic, where one condition can lead to one specific action.
Instructions:
- Discussion
You may say:
“Think about how you make decisions. For example, if it’s raining outside, what would you do? You’d probably grab an umbrella or stay indoors.”
Allow students to respond and share examples of how they make decisions based on changing circumstances.
You may say:
“In programming, we make decisions using something called an if statement. It’s like telling the robot, ‘If you see this, do that.’ Just like how you’d stay indoors if it’s raining, a robot can also take specific actions based on certain conditions.”
- Real-Life Example
You may say:
“Let’s try another example. Imagine a traffic light. If the light is red, cars stop; if it’s green, they go. Robots follow similar logic using if statements, so they can respond based on what’s happening around them.”
- Explain If-Then in Code Terms
You may say:
“If the robot ‘sees’ something, like an obstacle or any object, it can act on that information. The ‘if statement’ is a way to tell the robot, ‘If this is true, do this action.’”
Demonstrate a basic if statement on the projector. If we place an object at 5 cm, we can program the robot to turn and say “Obstacle Detected!” if the distance sensor detects something.
#include <M3DGo.h>
void setup()
{
go.begin();
}
void loop(){
if (range.getcm<5) {
go.display("Obstacle Detected!")
go.spinClockwise(); // Start Spinning Clockwise
delay(1000); // Wait 1 second
go.stop(); // Stop Spinning
go.forward(50); // Move forward if 50% speed
}
}
Teacher’s Role:
Guide the discussion toward understanding conditional logic in everyday scenarios and encourage students to think of other examples where they make decisions based on conditions. Ensure students grasp that if-then-else statements allow the robot to make choices based on sensor data.
2. Introducing If-Else Statements in Code (10 minutes)
Activity:
Explain the if-else structure in coding terms, demonstrating how it helps the robot make decisions between two options.
Instructions:
- Explain If-Else
You may say
“If-else statements are a way of giving the robot two choices. If a certain condition is true, it does one action. Else, or otherwise, it does something different.”
- Relatable Example
You may say:
“Let’s say you’re walking, and you have to decide which direction to go. If you see a path to your left, you turn left. Else, if there’s no path, you keep going forward. The robot can make choices just like this using if-else statements.”
- Write a simple code example on the board to illustrate:
#include <M3DGo.h>
void setup()
{
go.begin();
}
void loop(){
if (range.getcm<5) {
go.display("Obstacle Detected!") // Display text on the robot's screen
go.spinClockwise(); // Start Spinning Clockwise
delay(1000); // Wait 1 second
go.stop(); // Stop Spinning
go.forward(50); // Move forward if 50% speed
}
else {
go.display("No Obstacle Ahead!"); // Display text on the robot's screen
go.forward(100); // If no obstacle detected, keep moving forward at 100% speed
}
}
Teacher’s Role:
Demonstrate the if-else structure and check for understanding. Reinforce that an else statement provides an alternative action when the if condition is not met.
3. Using Sensors with If-Else Statements (15 minutes)
Activity:
Introduce the line sensor and distance sensor and discuss how the robot can use them to detect obstacles or follow a line. This will give students hands-on experience in using sensors with conditional logic.
Instructions:
-
Connecting the Line Sensor and Distance Sensor
- Guide students in attaching both the line and distance sensors to their robots.
-
Simple If-Else Example with Line Sensor
You may say:
“If the robot is on a black line, it will keep moving forward. Else, it will stop. Let’s code this together!”
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (line.onTheLine()) { // Check if robot is on the line
display.text("On the Line"); // Display message
go.forward(50); // Move forward at 50% speed
} else {
display.text("Off the Line"); // Display message
go.stop(); // Stop the robot
}
go.delay(500); // Short delay for stability
}
- Distance Sensor Example
You may say:
“Now let’s add a distance sensor to detect obstacles. If an obstacle is detected within a certain range, the robot will stop and display a message on the screen.”
Example Code:
void setup() {
go.begin();
}
void loop() {
if (range.get_cm() < 20) { // If obstacle detected within 20 cm
display.text("Obstacle Ahead!"); // Display warning
go.stop(); // Stop the robot
} else {
display.text("Clear Path"); // Display message
go.forward(50); // Move forward
}
go.delay(500); // Short delay
}
Teacher’s Role:
Guide students through setting up and testing each sensor-based decision. Check that students understand how the conditions in if-else statements affect the robot’s behavior based on sensor input.
4. Hands-On Challenges (20 minutes)
Encourage students to use the new if-else structure with sensor input and displays to create simple decision-making logic that controls the robot’s movements based on its surroundings.
Challenge 1: Obstacle Detection and Stop
- Task: Program the robot to move forward and stop if it detects an obstacle within 20 cm.
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (range.get_cm() < 20) { // Check if an obstacle is detected within 20 cm
display.text("Obstacle Detected"); // Display a warning
go.stop(); // Stop the robot
} else {
display.text("Clear Path"); // Display message
go.forward(50); // Move forward at 50% speed
}
go.delay(500); // Short delay for stability
}
Challenge 2: Safe Distance Follow
- Task: Program the robot to move forward if there’s no obstacle within 15 cm. If an obstacle is detected closer than 15 cm, make the robot slow down and display “Slowing Down.”
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (range.get_cm() < 15) { // If obstacle is closer than 15 cm
display.text("Slowing Down"); // Display message
go.forward(30); // Slow down to 30% speed
} else {
display.text("Moving Forward"); // Display message
go.forward(50); // Move forward at 50% speed
}
go.delay(500); // Short delay for stability
}
Challenge 3: Back Away from Obstacles
- Task: Program the robot to move forward, but if it detects an obstacle within 10 cm, it should back up for 2 seconds and display “Backing Up.”
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (range.get_cm() < 10) { // If obstacle is very close
display.text("Backing Up"); // Display warning
go.reverse(50); // Move backward at 50% speed
go.delay(2000); // Back up for 2 seconds
go.stop(); // Stop
} else {
display.text("Moving Forward"); // Display message
go.forward(50); // Move forward at 50% speed
}
go.delay(500); // Short delay for stability
}
Challenge 4: Warning at a Safe Distance
- Task: Program the robot to display “Safe Distance” while moving forward if there’s no obstacle within 20 cm. If an obstacle is detected between 10 and 20 cm, display “Caution: Close” but keep moving at a reduced speed. If closer than 10 cm, stop and display “Too Close!”
Example Code:
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
if (range.get_cm() < 10) { // If obstacle is very close
display.text("Too Close!"); // Display message
go.stop(); // Stop the robot
} else if (range.get_cm() < 20) { // If obstacle is moderately close
display.text("Caution: Close"); // Display message
go.forward(30); // Move at reduced speed
} else {
display.text("Safe Distance"); // Display message
go.forward(50); // Move forward at normal speed
}
go.delay(500); // Short delay for stability
}
Teacher’s Role:
Guide students as they work through each challenge, ensuring they understand the purpose of each conditional statement and how the robot’s actions are determined by sensor input. Encourage students to test and troubleshoot their code, observing how the robot behaves in response to different distances from obstacles.
5. Reflection and Wrap-Up (5 minutes)
Activity:
Conclude the session with a reflection on how conditionals helped students control the robot’s actions based on sensor input.
Discussion Questions:
- You may ask: “How did using if-else statements help our robot make decisions?”
- You may ask: “What was the most challenging part about getting the robot to respond to sensor data?”
- You may say: “Next time, we’ll explore how to use more complex decisions with more sensors , so the robot can handle even more situations on its own!”
Teacher’s Role:
Lead the reflection discussion, allowing students to share their experiences and insights. Reinforce how the concepts they learned will be useful as they tackle more complex challenges in future sessions.