[RL64] Grade 6 - Session 4

Session 4: Line Tracking 101

Session Architect: Muhammad Bilal
Duration: 50-60 minutes


Equipment Required:

  • M3D Robotics kit with line sensor attached
  • Laptops with Arduino IDE pre-installed
  • USB/OTG cables to connect the robot to the laptop
  • Black tape to create a straight line on a white surface
  • Paper and pencils for students to document their experiments

Learning Objectives:

  • Understand the concept of conditional logic using if statements.
  • Learn how to use comparison operators (greater than, less than, equal to) to check conditions in code.
  • Program the robot to follow a black line using the line sensor and basic conditional logic.
  • Test and adjust the code to ensure smooth movement along the line.

1. Introduction to Conditionals and Decision-Making with If (5 minutes)

Activity:
Start the session with a discussion about how decisions are made based on certain conditions. This will introduce the concept of if statements, where one condition leads to one action.

Objective:
Help students understand how robots make decisions using simple if statements to perform actions based on conditions.

Instructions:

  1. Set the Stage:

Begin by asking:

“How do you decide what to do if something specific happens? For example, if it’s raining, what do you do? '”

They might say, “If it rains, I’ll use an umbrella.” You can ask them why did they decide to take that action? What changed their decision to go without an umbrella? This will make them understand the idea of condition based decision making.

  1. Relate to Robot Decision-Making:

Explain how robots also make decisions based on conditions.

“Just like you decide what to do based on whether or not it’s raining, robots can decide what to do based on whether or not some condition is true or not. Such as, if the robot is moving at a certain speed or if the robot detected something, like a line on the ground.”

  1. Introduce If Statements:

Explain how an if statement works:

“An if statement checks whether a condition is true. If it is, the robot will do something. If not, it won’t do anything.”

  1. Everyday Example:
    • Give a simple example that relates to their daily life:
      “If it’s raining, I’ll use my umbrella.”
      Explain that this is like the robot checking a condition:
      “If I see the line, I’ll move forward.”

Teacher’s Role:

  • Guide students through real-life examples of decision-making to introduce the concept of an if statement.
  • Explain how the robot checks a condition with an if statement to decide what action to take.
  • Ensure that students understand that only one condition is being checked in an if statement.

2. Using If Statements in Basic Robot Movement (10 minutes)

Activity:
Introduce the concept of comparison operators, explaining how we can check whether something is greater than, less than, or equal to a specific value. This will prepare students for more complex conditional logic later on. Using these operators, students will write basic code using if statements to control the robot’s movement. They will experiment with checking the robot is at the desired speed and move forward if the condition is true.

Instructions:

  1. Explaining Comparison Operators:

Explain how we can use comparison operators to check conditions:

“We can use comparison operators to check if something is greater than, less than, or equal to a certain value. For example, we might check if the robot’s speed is greater than 50.”

  1. Examples of Operators:

    • Introduce common comparison operators:
      • greater than ( > )
      • less than ( < )
      • equal to ( == )
  2. Real-Life Example:

Give a real-life example of using comparison:

“If the temperature is greater than 30°C, you might decide to turn on a fan. In the same way, we can check if the robot’s speed is greater than a certain value to decide how fast it should move.”

  1. Basic Movement with If:

Show students how to use an if statement to control movement based on whether the robot is at the desired speed:


void setup() {
    go.begin();  // Initialize the robot 
    int speed = 50;
}


void loop() {
    if (speed = = 50) {
        go.forward();  // Move forward if the robot is set at the desired speed
    }
}
  1. Explaining the Condition:

Explain how the robot checks the condition:

“Here, the robot checks whether it is on the line. If the condition is true, it moves forward. If the condition isn’t true, the robot doesn’t do anything.”

Also explain how they should write the condition in terms of syntax. They should follow the following steps:

  • Start with writing if following by parenthesis and then curly brackets:
   if () {
   }
  • The condition goes inside the small brackets and the action that the robot should take is written inside the curly brackets.
  1. Testing the Code:
    • Ask students to upload the code to their robot and test how it behaves when placed on a black line. Encourage them to observe how the robot only moves forward when it detects the line.

Teacher’s Role:

  • Explain comparison operators and how they are used to check different conditions.
  • Use relatable examples to help students understand the role of comparison operators in coding.
  • Make sure students are comfortable with the concept before moving on to more complex conditionals.
    Help students understand how to check a single condition using an if statement.
  • Guide them through testing their code and observing how the robot responds when the condition is true.
  • Answer any questions about how the if statement works in controlling the robot’s behavior.

4. If Statements in Basic Movement and Variables (15 minutes)

Activity: Now that students have learned how to use if statements, the teacher will guide them to practice using these conditions with basic movement and variables. This segment will focus on simple movement-based tasks to solidify their understanding of conditionals and variables.

Instructions:

  1. Introducing the Challenges:
  • The teacher will now give the students simple challenges that use if conditions with movement. These challenges are designed to help students practice checking conditions and making decisions in their code.
  • For example:
    • Challenge 1: If the robot’s speed is greater than 50, it should move forward.
    • Challenge 2: If the robot’s speed is less than or equal to 30, it should spin clockwise.
    • Challenge 3: If the speed is exactly 75, the robot should move forward and stop after 3 seconds.
  1. Writing the Code:
  • Students will write code for each of these challenges. The teacher will explain how they can incorporate the variables and conditions they’ve learned so far. For example:

Challenge 1:

int speed = 60;  // Variable for speed

void setup() {
    go.begin();  // Initialize the robot
}

void loop() {
    if (speed > 50) {
        go.forward(speed);  // Move forward if speed is greater than 50

    }
}

Challenge 2:

int speed = 25;  // Variable for speed

void setup() {
    go.begin();  // Initialize the robot
}

void loop() {
    if (speed <= 30) {
        go.spinClockwise(speed);  // Spin clockwise if speed is 30 or less
    } 
    
}

Challenge 3:

int speed = 75;  // Variable for speed

void setup() {
    go.begin();  // Initialize the robot
}

void loop() {
    if (speed == 75) {
        go.forward(speed);  // Move forward at speed 75
        delay(3000);  // Wait for 3 seconds
        go.stop();  // Then stop
    }
}

Testing and Adjusting:

  • Students will upload their code and test their robots to see if they meet the challenge requirements. They will be encouraged to observe the robot’s behavior and adjust their code as necessary.
  • The teacher can ask students to test different values for the speed variable to see how changing it impacts the robot’s movement.

Teacher’s Role:

  • Provide the challenges to students and explain the goal of each one.
  • Guide them in writing code that uses if statements and variables for movement control.
  • Encourage students to test their code and observe how the robot’s behavior changes depending on the conditions they’ve written.
  • Offer help as needed, explaining why certain conditions lead to specific outcomes.

5. Introducing the Line Sensor (10 minutes)

Activity:
Students will be introduced to the Line Sensor and learn how to use it in their code. They will connect the robot to the line sensor and use the line.onTheLine() command to check if the robot is on the line.They will then write code to alter the robot’s behavior based on whether it’s on the line or not.

Instructions:

  1. Introducing the Line Sensor:

Begin by asking students how they think a robot might follow a line in the real world. Engage them in a discussion:

“Imagine you’re walking along a path, and your goal is to stay on that path. How would you know if you’ve stepped off it? Now, imagine how a robot might stay on a path using its ‘senses.’ Our robot has a special sensor that helps it follow a line. It’s called a line sensor.”

  1. Explaining the Line Sensor:

Explain how the line sensor works:

“The line sensor uses two detectors to sense the line. If both of them sense the line, the robot knows it’s on the path. If one of the sensors can’t detect the line, the robot knows it has gone off course.”

  1. Introducing the Command:

Show students how to check if the robot is on the line using the line.onTheLine() command:

“The command line.onTheLine() will return a value of ‘true’ if both sensors are detecting the line. We can use this in our code to tell the robot to behave differently based on whether it’s on the line or not.”

Encourage them to look at the code and notice how just like the all movement instructions start with “go.”, the line sensor commands start with “line.”

  1. Connecting the Line Sensor:

Ask students to connect the robot to the line sensor. You may want to demonstrate this for them.

“Let’s go ahead and connect our line sensor. Once it’s connected, we’ll use our new command to check if the robot is on the line and make it behave accordingly.”

  1. Writing the Code:

Guide students through writing a simple program that alters the robot’s behavior based on whether it’s on the line or not. For example, the robot can move forward when it’s on the line and stop when it goes off the line:

void setup() {
    go.begin();  // Initialize the robot
}

void loop() {
    if (line.onTheLine()) {  // Check if the robot is on the line
        go.forward(50);    // Move forward at 50% speed
    } 
}
  1. Explaining the Code:

Explain to the students:

“Here, the robot is constantly checking if it’s on the line. If the line sensor detects the line, the robot moves forward. If the robot strays off the line, it stops. This is how we can make the robot follow a path using its sensor.”

  1. Testing the Code:

Ask students to test the code by running the program and observing how the robot behaves when it’s on and off the line.

“Let’s test the code now! Place your robot on the black line and see how it behaves. Does it move when it’s on the line and stop when it’s off?”


Teacher’s Role:

  • Lead the discussion about how the line sensor works and its importance in helping the robot follow a path.
  • Help students connect the line sensor to the robot.
  • Guide them in writing and testing the code, offering assistance if they run into issues.
  • Encourage students to think critically about how the robot behaves based on whether it’s on the line or not.

6. Using If-Else Statements to Check Multiple Conditions (10 minutes)

Activity:
Students will now extend their understanding by using if-else statements. They’ll learn how to check multiple conditions and control the robot’s behavior based on different scenarios.

Instructions:

  1. Introducing If-Else:

Explain how if-else works:

“If the first condition is true, the robot will perform one action. Else, it will do something else. For example, we can tell the robot to move if the speed is greater than 50, and stop if it’s not.”

  1. Basic If-Else Example:

    • Show the students how to use an if-else statement in their code:
    void setup() {
        int speed = 50
        go.begin();  // Initialize the robot
    }
    
    void loop() {
        if (speed = = 50) {
            go.forward();  // Move forward if the robot's speed is 50%
        } else {
            go.stop();  // Stop if it is not
        }
    }
    
  2. Explaining the Code:

Explain how the robot checks the conditions:

“If the robot’s set speed is 50, it moves forward. Else, if it’s not, it stops.”

  1. Testing the Code:

Ask students to upload the code and test how the robot behaves on and off the line. Encourage them to observe how the robot stops when it detects it’s no longer on the line.

Once they see the code in action, ask them to code a situation on their own. You could give them practice problems such as:

Challenge 1: Speed Control Based on Distance

Students will write a program that adjusts the robot’s speed based on how far it’s traveled. If the robot has traveled less than 50 cm, it will move at full speed. Else, it will slow down.

Example Code:

int distanceTraveled = 60;  // Variable for distance traveled in cm

void setup() {
    go.begin();  // Initialize the robot
}

void loop() {
    if (distanceTraveled < 50) {
        go.forward(100);  // Move at full speed if distance is less than 50 cm
    } else {
        go.forward(30);  // Slow down if distance is greater than or equal to 50 cm
    }
}

Challenge 2: Obstacle Avoidance and Speed Adjustment

In this challenge, the robot will move forward at a set speed. If it is on the line, it will start moving at 100% speed. Otherwise, it will go back to the original speed and will continue moving.

Example Code:

void setup() {
    go.begin();  // Initialize the robot
}

int speed = 70;
void loop() {
    go.forward(speed);
    if (line.onTheLine()) {
        go.forward(100);  // Move faster if a line is detected
    } else {
        go.forward(speed);  // Move with original speed if no line detected
    }
}

Challenge 3: Line Following with Speed Variations

In this challenge, students will program the robot to move at different speeds based on whether it’s on the line or off it. If the robot is on the line, it will move forward at full speed. If the robot is off the line, it will gradually slow down and stop. Students will also incorporate a delay to simulate real-time responses.


Example Code:

int fastSpeed = 100;  // Full speed when on the line
int slowSpeed = 30;   // Slower speed when off the line
int stopDelay = 500;  // Time delay before stopping

void setup() {
    go.begin();  // Initialize the robot
}

void loop() {
    if (line.onTheLine()) {  
        go.forward(fastSpeed);  // Move forward at full speed if on the line
    } else {  
        go.forward(slowSpeed);  // Slow down if not on the line
        delay(stopDelay);       // Wait for half a second before stopping
        go.stop();              // Stop the robot after delay
    }
}

Teacher’s Role:

  • Guide students through writing and testing these challenges.
  • Explain the logic of if-else statements and how they can be used to check multiple conditions.
  • Encourage students to test different values in their code and observe how the robot responds to changes in the conditions.
  • Assist students with troubleshooting if their code doesn’t behave as expected.

7. Reflection and Wrap-Up (5 minutes)

Activity:
Wrap up the session by reviewing the key concepts of conditional logic and how they applied to the robot’s behavior.


Instructions:

  1. Recap the Key Learnings:
    Briefly review the use of if and if-else statements:

“Today, we learned how robots make decisions based on conditions, just like we do in daily life. The robot can decide whether to move or stop based on what it detects, such as staying on the line or adjusting its speed.”

  1. Encourage Reflection:

Ask students:

  • “What did you notice when the robot followed the line or changed its speed?”
  • “Why is it important for a robot to use conditions like this?”
  1. Highlight Testing and Adjustment:

“We saw how testing and adjusting our code is key to getting the robot to do exactly what we want. Even small changes can make a big difference.”

Teacher’s Role:

  • Lead a concise reflection discussion.
  • Encourage students to think critically about how the robot responded to different conditions.
  • Prepare students for the next session by linking today’s learning to future concepts.