Session 3: Endless Moves: Loop Logic
Duration: 50-60 minutes
Equipment Required:
- M3D Robotics kit (M3D Go)
- Laptops with Arduino IDE
- USB/OTG cables to connect the robot to the laptop
- Projector for live coding demonstrations
Learning Objectives:
- Understand variables as containers for storing data values.
- Explore basic types of variables and how to use them in code.
- Learn for loops for repeating actions a specified number of times.
- Apply loops with movement, display messages, and sensor readings to control the robot’s behavior.
1. Introduction to Variables (15 minutes)
Activity:
Start with a hands-on activity to introduce the concept of variables as a way to store and manipulate data in code. This sets up a foundation for using variables with loops.
Instructions:
- Visualizing Variables:
Begin with an empty container or cup, explaining that variables are like containers where we store values temporarily. You can start as follows:
"Think of this container as a variable. Right now, it’s empty, but we can add coins to it, just like putting points into a game score. When we need the value, we can pull it out or change it.”
- Write “Coins:” on the board and hand out coins to students.
- Encourage them to add the coins in the cup and change the value of the variable “Coins” as they add coins in it.
- Demonstrate Variable Usage:
Write code to create and assign a simple variable on the projector.
- Example Code:
int score = 0; // Creates a variable named 'score' and sets it to 0
Explain that int means it’s a whole number. You may say:
“Here, ‘score’ is a variable that starts at zero. As we play, we can increase this score. Just like you count points in a game, we count values with variables.”
- Types of Variables:
Introduce a few basic variable types with examples:- int (integer): Stores whole numbers, like scores or counts.
- float (floating-point): Stores numbers with decimals, like distances or measurements.
- String: Stores text, like messages to display on the screen.
- bool: Stores the status true or false of a state to be used in conditions.
- Example Code:
int count = 5; // Stores a whole number float distance = 15.6; // Stores a number with decimals String greetings = "Hello"; // Stores text
You can explain each variable as you code them on the projector screen:
“Just like you can put different items in a container, you can store different kinds of data in variables.
intis for whole numbers,floatfor decimals, andStringfor text.”
2. Introduction to Loops (10 minutes)
Activity:
Begin with a discussion on the need for repeating actions in code, introducing for loop.
Discussion Examples:
You may say:
“Have you ever clapped ten times in a row or counted steps? Instead of writing the same instruction multiple times, we can say ‘Clap ten times,’ and everyone knows what to do. A for loop helps us repeat actions like this.”
Introduce for loops and demonstrate a basic example on the projector.
You main explain the syntax by breaking it down in three parts:
- Variable initialization/Defining the variable
- Condition
- Variable change
for(i = 0, i<10, i++){
display.text("Clap!"); // Instructions to be repeated
}
Example Code for For Loop:
#include <M3DGo.h>
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
for (int i = 0; i < 3; i++) { // Repeat actions 3 times
go.forward(50); // Move forward at 50% speed
go.delay(1000); // Move for 1 second
go.stop(); // Stop
go.delay(500); // Wait for 0.5 seconds
}
}
Explanation:
You may say:
“This for loop repeats three times. Each time, the robot moves forward, shows ‘Step’ and the step number, waits, then stops. The
ivariable keeps count.”
Encourage students to change the loop count and observe the changes.
3. Hands-On Challenges (30 minutes)
Encourage students to apply for loop, using movement, display messages, and distance readings.
Challenge 1: Forward and Spin Repeat
- Task: Program the robot to move forward and spin clockwise, repeating five times using a for loop.
Example Code:
void setup() {
go.begin();
}
void loop() {
for (int i = 0; i < 5; i++) {
go.forward(50);
display.text("Forward Step " + String(i+1));
go.delay(1000);
go.spinClockwise(50);
display.text("Spinning");
go.delay(500);
go.stop();
}
}
Challenge 2: Loop Counter on Display
- Task: Use a for loop to make the robot move forward 3 times and display the loop count each time.
Example Code:
void setup() {
go.begin();
}
void loop() {
for (int i = 1; i <= 3; i++) {
go.forward(50);
display.text("Loop " + String(i)); // Display loop count
go.delay(1000);
go.stop();
go.delay(500);
}
}
Challenge: Obstacle Response with State Variable
Task:
Program the robot to detect obstacles within 15 cm using the distance sensor. When it detects an obstacle, set a boolean variable (obstacleDetected) to true. If obstacleDetected is true, the robot should stop, display “Obstacle Ahead!” on its screen, and spin in place for 2 seconds. Once the spin is complete, reset obstacleDetected to false, allowing the robot to resume moving forward if the path is clear.
Goal:
Learn to use a boolean variable to create a state that triggers specific actions based on sensor input.
Example Code:
#include <M3DGo.h>
void setup() {
go.begin();
}
void loop() {
bool obstacleDetected = false; // Variable to track obstacle status
// Check if obstacle is detected within 15 cm
if (range.get_cm() < 15) {
obstacleDetected = true; // Set state to true if obstacle is close
}
// Perform actions based on obstacle state
if (obstacleDetected) {
go.stop(); // Stop the robot
display.text("Obstacle Ahead!"); // Display message
go.spinClockwise(50); // Spin in place
go.delay(2000); // Spin for 2 seconds
obstacleDetected = false; // Reset state after action
} else {
go.forward(50); // Continue moving if no obstacle
}
}
4. Reflection and Wrap-Up (5 minutes)
Reflection Questions:
You may ask:
- “How did using a for loop help you repeat actions a specific number of times?”
- “What did you find challenging or interesting about today’s session?”
Teacher’s Role:
Wrap up with a discussion, reinforcing the value of variables and loops for creating efficient, flexible code that can adapt to repeated actions and specific conditions.
Learning Outcome:
By the end of this session, students will understand basic variables, for loop, and use these structures in conjunction with sensor inputs and display messages to build decision-based behaviors in the robot.