[RL82] Grade 8 - Session 2

Session 2: Exploring Sensor Data and Interactive Display

Duration: 60 minutes


Equipment Required:

  • M3D Robotics kit with distance and line sensors
  • Laptops with Arduino IDE pre-installed
  • USB/OTG cables to connect the robot to the laptop
  • Projector for live code demonstration
  • Various surfaces (e.g., paper, fabric, reflective materials) for experiments

Learning Objectives:

  • Understand how sensors gather data and display real-time readings.
  • Learn how to use commands to show sensor values on the robot’s screen and the computer’s terminal.
  • Experiment with different surfaces to observe sensor response and accuracy.
  • Introduce basic conditional statements to make the robot respond to sensor data.

1. Introduction to Machine Senses (5 minutes)

Activity:
Begin with an engaging discussion on how machines “sense” their surroundings.

Discussion Points:

  1. Ask students to brainstorm how machines might interact with the environment. You may ask:

“How do you think a machine, like a robot vacuum or a self-driving car, knows where to go?”

“What ways might a machine ‘feel’ or ‘see’ what’s around it?”

  1. Have students write down their ideas on how machines could detect objects, track paths, or respond to user commands.

  2. Summarize their responses, emphasizing how sensors act as a machine’s “eyes” and “ears,” helping it understand the world around it.

Teacher’s Role:
Guide students to connect their ideas to the sensors on their robot, setting the foundation for experimenting with real-world sensor data.


2. Introducing the Sensors (5 minutes)

Activity:
Introduce the distance sensor and line sensor and explain their roles.

Instructions:

  1. Distance Sensor: Explain how the distance sensor measures how far an object is from the robot.

    • Commands:
      • range.get_mm() - measures in millimeters
      • range.get_cm() - measures in centimeters
      • range.get_ft() - measures in feet
  2. Line Sensor: Explain that the line sensor has two infrared (IR) sensors, one on the left and one on the right, which detect contrast between different surfaces.

    • Commands:
      • line.onTheLine() - Checks if the robot is on the line
      • line.notOnLine() - Checks if the robot is not on the line
      • line.getLeft() - Gives the value of left sensor of the line detector
      • line.getRight() - Gives the value of right sensor of the line detector

Teacher’s Role:
Demonstrate the sensors in action by displaying values on the robot’s screen and discussing how these readings change based on proximity (for the distance sensor) or surface type (for the line sensor).


3. Displaying Sensor Data (10 minutes)

Activity:
Walk students through using commands to display sensor readings on the robot’s screen and on the computer’s terminal.

Instructions:

  1. On the Robot’s Screen:
    • Use display.text() to show sensor readings directly on the robot.
  2. On the Computer Terminal:
    • Introduce Serial.println() to show sensor readings on the computer’s terminal, a useful way to monitor data as it’s collected.

Example Code:

#include <M3DGo.h>

void setup() {

    go.begin();

}

void loop() {
    
    display.text("Dist: " + range.get_cm() + " cm");  // Display distance on screen
    Serial.println("Distance (cm): " + range.get_cm());  // Display on terminal

    delay(1000);  // Wait for 1 second before updating
}
  • Encourage students to play around with the delay so that they can see the changing sensor values in real-time.

Teacher’s Role:
Guide students as they try out each command, helping them observe how the sensor readings display differently across each platform.


4. Experimenting with Sensors on Different Surfaces (10 minutes)

Activity:
Students will test the sensors on various surfaces and distances to see how sensor readings vary in different conditions.

Instructions:

  1. Divide students into small groups, and provide each group with different surfaces (e.g., white paper, dark cloth, reflective foil).
  2. Have students place their robot on each surface, noting how the line sensor values change.
  3. Ask them to place objects at different distances from the robot and observe the distance sensor’s accuracy.

Teacher’s Role:
Encourage students to record their observations. You may ask:

“Does the distance sensor read differently if an object is too close?”

“How does the line sensor reading change on light vs. dark surfaces?”


5. Introducing If Logic for Decision-Making (10 minutes)

Activity:
Begin by introducing students to the idea of conditional statements using one simple condition, and then gradually build up to checking two conditions at once.

Instructions:

Step 1: Starting with One Condition

Begin with a relatable discussion to help students understand how conditions work.

  • You may ask:

“Imagine you’re walking down a path, and you decide to only keep moving if the path is clear. If there’s an obstacle, you stop. Does that make sense?

  • Explain that the robot can be programmed to do the same: it can move forward only if a certain condition (like a clear path) is met.

  • On the projector, write a simple example code where the robot moves forward if there’s no object within 15 cm.

Example Code for One Condition:

#include <M3DGo.h>

void setup() {
    go.begin();
}

void loop() {
    if (range.get_cm() > 15) {  // Condition: No obstacle within 15 cm
        go.forward(50);         // Move forward if path is clear
        display.text("Moving Forward");
    } else {
        go.stop();              // Stop if there's an obstacle
        display.text("Obstacle Ahead!");
    }
    delay(500);
}

Explanation:

You may say:

"In this code, we’re telling the robot, ‘Move forward if there’s nothing within 15 cm.’ If something is closer, the robot stops and displays ‘Obstacle Ahead!’”

Encourage students to try different distances for the condition (like 10 cm or 20 cm) to see how changing the condition affects the robot’s behavior.


Step 2: Moving to Two Conditions

  • Once students understand one condition, introduce the concept of using two conditions together.
  • You may ask:

“Now, let’s say you’re walking on a line. You’ll only move forward if there’s no obstacle in front and you’re still on the line. What happens if one of these conditions isn’t true?”

  • Lead them to understand that in programming, we can check multiple conditions at the same time to make sure both are true before taking an action.

Explanation of the && (AND) Operator:
Explain that using && between two conditions means both conditions must be true for the code inside the if block to run.

Example Code for Two Conditions:
In this example, the robot will move forward only if it’s on the line and there’s no obstacle within 15 cm.

#include <M3DGo.h>

void setup() {
    go.begin();
}

void loop() {
    if (line.onTheLine() < 50 && range.get_cm() > 15) {  // Two conditions: On line and clear path
        go.forward(50);                                // Move forward if both are true
        display.text("Moving on Line");
    } else {
        go.stop();                                     // Stop if any condition is false
        if (range.get_cm() <= 15) {
            display.text("Obstacle Ahead!");
        } else {
            display.text("Off Line!");
        }
    }
    delay(500);
}

Explanation:

You may say:

"Now, we’re telling the robot, ‘Move forward if both of these are true: you’re on the line and there’s no obstacle.’ If one condition isn’t met—if there’s an obstacle or the robot isn’t on the line—it stops.”

Encourage students to test this code by placing the robot on and off the line or placing objects in front of it to observe how the two conditions work together.


Teacher’s Role:

  • Guide students through each code example, helping them see how adding more conditions allows for more specific actions.
  • Use real-world analogies to explain &&, like needing two green lights at an intersection (one for going straight and another for turning) before proceeding.
  • Reinforce the concept by asking students to experiment with other conditions, like adjusting the distance or using the right line sensor instead of the left.

This progression will help students build a solid understanding of how conditional logic enables the robot to make decisions based on its environment.


6. Hands-On Challenges (15 minutes)

Challenge 1: Detect and Display Distance

  • Task: Program the robot to display the distance from an object on both the robot’s screen and computer terminal.
  • Goal: Practice using range.get_cm() and display.text() together.

Example Code:

void loop() 

    display.text("Distance: " + range.get_cm() + " cm");
    serial.println("Distance (cm): " + range.get_cm());
    delay(1000);

}

Challenge 2: Line Sensor Reading

  • Task: Program the robot to display “On Track” if both line sensors detect a dark line, and “Off Track” if one or both do not.
  • Goal: Use the line sensor commands and practice if statements.

Example Code:

void loop() {

    if (line.getLeft() < 50 && line.getRight() < 50) {  // Assume a dark line reads below 50
        display.text("On Track");
    } else {
        display.text("Off Track");
    }
    delay(500);
}

Challenge 3: Conditional Stopping

  • Task: Program the robot to move forward and stop if it detects an object within 15 cm.
  • Goal: Apply if logic to control movement based on sensor input.

Example Code:

void loop() {

    if (range.get_cm()< 15) {    // If an object is closer than 15 cm
        go.stop();
        display.text("Obstacle!");
    } else {
        go.forward(50);
    }
    delay(500);
}

Challenge 3: Accurate Path Following with Obstacle Response

  • Task: Program the robot to follow a line and pause when it encounters an obstacle within 10 cm or it gets off the line. It will then check whether it was an obstacle that made the robot stop or did it lose the path. Display “Path Clear” when moving and “Obstacle on Path” if both conditions are met.

  • Goal: Develop precision in navigation by using both distance and line sensors to ensure the robot stays on its path while avoiding obstacles.

Example Code:

void loop() {

    if (range.get_cm() > 10 && line.notOnLine()) {  // Obstacle farther than 10 cm and on the path
        go.forwrad();
        serial.println("Path Clear");
    } else if (range.get_cm<10){
        serial.println("Path Blocked");
    } else if (line.notOnLine) {
        serial.println("Off the path!");
    }
    delay(500);
}

Teacher’s Role:
Encourage students to experiment with each challenge and provide support as they explore how the if statements impact robot behavior.


7. Conclusion and Reflection (5 minutes)

Reflection Questions:

  • You may ask:
    • “What did you notice about how the sensors read values on different surfaces or distances?”
    • “How could you use the if logic to make the robot more interactive or responsive?”

Teacher’s Role:
Guide students in reflecting on how sensors help robots make informed decisions, using conditions to respond in various ways.


Learning Outcome:

By the end of this session, students will understand how to collect sensor data, display readings on various outputs, and use basic if logic to respond to different conditions. They will develop an intuitive understanding of how sensors enable interaction and data-based decision-making in robotics.