Session 3: Coding the Bot’s Brain – Introduction to Variables
Session Architect: Muhammad Bilal
Duration: 50-60 minutes
Equipment Required:
- M3D Robotics kit
- Laptops with Arduino IDE pre-installed
- USB/OTG cables to connect the robot to the laptop
- Paper and pencils for students to document their experiments
Learning Objectives:
- Understand the concept of variables and their role in coding.
- Learn to create and modify variables in Arduino code to control the robot’s speed and rotation.
- Experiment with different speeds and observe the robot’s performance on various terrains.
1. Introduction to the Concept (10 minutes)
Activity:
Start with a fun, interactive activity to show students how we can keep track of changing values, using a simple task that’s easy to understand and engaging.
Objective:
Help students grasp the idea of tracking and updating information over time, using an example of counting objects.
Instructions:
- Set the Stage:
- Call a student to the front of the class and place a pile of small objects, like books or markers, on a table.
- On the board, write: “Books currently carried by [Student’s Name]: 0”
Tell the class:
“Right now, [Student’s Name] isn’t carrying any books, so the number is 0. But that number might change as we go along, and we need to keep track of it.”
- Demonstrate Tracking Changes:
- Hand two books to the student and update the number on the board to:
“Books currently carried by [Student’s Name]: 2”
- Hand two books to the student and update the number on the board to:
Ask the class:
“What happened? The number changed because [Student’s Name] is now carrying 2 books.”
- Modify the Value:
- Give the student two more books and update the number on the board to:
“Books currently carried by [Student’s Name]: 4”
- Give the student two more books and update the number on the board to:
Ask the class:
“How do we know how many books they’re carrying? We’ve been keeping track and updating the number, right?”
Emphasize that the number is changing based on the actions performed (adding books), and you’re keeping track by updating the total.
- Increase Engagement:
- Now ask the student to place one book down. Update the number on the board to:
“Books currently carried by [Student’s Name]: 3”
- Now ask the student to place one book down. Update the number on the board to:
Then ask the class:
“What happened now? The number went down because one book was put back. We’re still keeping track, but now it’s decreasing instead of increasing.”
- Introduce the Name:
- After the students understand how you’ve been keeping track of how many books are being carried, explain:
“In coding, we need to keep track of changing information like this all the time. We use something called a ‘variable’ to do this. A variable is just like the number we’ve been updating—it stores information that can change, like how many books are being carried, how fast a robot moves, or how long it spins for.”
- Connect to the Robot:
- Finish by saying:
“Our robot also keeps track of things, like its speed or how far it’s moved, using variables. Let’s explore how we can create these variables in our code to control the robot’s behavior!”
Teacher’s Role:
- Lead the activity and ensure students understand the concept of tracking and updating values.
- Help students recognize how the total changes based on actions, preparing them to understand the concept of variables.
- Guide the discussion toward the idea that we need a way to keep track of changing values in coding, introducing the term ‘variable’ only after the concept is clear.
- Make connections to how variables are used in coding to track things like speed, time, or distance traveled by the robot.
2. Using Variables in Code (10 minutes)
Activity:
Students will learn how to create variables in their Arduino code and use them to control the robot’s speed and rotation. They will write simple code that adjusts the robot’s movements based on the values stored in variables.
Instructions:
-
Introducing Variables in Code:
Show the students how to create a variable to store the robot’s speed. You can start with an example:#include <M3DGo.h> int speed = 50; // Create a variable for speed void setup() { go.begin(); // Start the robot } void loop() { go.forward(speed); // Use the variable for speed }Explain:
“Here, we’ve created a variable called ‘speed,’ and it’s set to 50. Instead of putting the number directly in the movement command, we use the variable to control the speed.”
- Changing Variables:
Demonstrate how changing the value of the variable changes the robot’s behavior:
“If we change ‘speed’ to 30, the robot will move slower. If we change it to 100, it will move at full speed!”
- Experiment:
Challenge the students to modify the speed variable and see how it affects the robot’s movement.
Teacher’s Role:
- Guide students through creating and using variables in their code.
- Encourage them to experiment by adjusting the speed variable and observe how the robot’s movement changes.
- Provide assistance if they struggle with writing or modifying the code.
3. Experimenting with Speed on Different Terrains (15 minutes)
Activity:
Students will now test how the robot performs on different surfaces (e.g., smooth, rough) by adjusting the speed variable. This will help them understand how speed affects the robot’s ability to navigate various terrains.
Instructions:
-
Setting Up the Terrain:
Set up areas in the classroom with different surfaces for the robot to move on (e.g., tiles, carpet, or a smooth table). -
Testing with Variables:
Ask students to modify the speed variable and observe how the robot’s movement changes on each surface:
“Let’s see how the robot moves on the carpet compared to the tile floor when we set the speed to 30, 50, and 100.”
- Recording Observations:
Have students document their findings. They should note how the robot’s speed impacts its ability to move on different surfaces.
Teacher’s Role:
- Help students set up their terrain and guide them in testing different speeds.
- Encourage students to make predictions about how the robot will perform on each surface.
- Facilitate a discussion about how different surfaces affect the robot’s movement at various speeds.
4. Using Variables for Rotation (10 minutes)
Activity:
Students will extend their use of variables to control the robot’s rotation. They will learn how to store rotation speed in a variable and adjust it in their code.
Instructions:
-
Creating a Rotation Variable:
Show the students how to create a variable for rotation speed:int rotationSpeed = 60; // Variable for rotation speed -
Using the Rotation Variable:
Demonstrate how to use this variable to control the robot’s rotation:go.spinClockwise(rotationSpeed); // Use the variable for rotation speed -
Experiment:
Ask students to modify the rotation speed variable and observe how quickly the robot spins.
Teacher’s Role:
- Guide students through creating and using a rotation speed variable.
- Encourage them to experiment with different rotation speeds and observe the impact on the robot’s movement.
- Answer questions and provide support as they experiment.
Apologies for that! Let’s focus on challenges that align with the basic concepts being introduced in the session, specifically using variables to control the robot’s speed and rotation without conditions or variable increments.
5. Coding Challenges with Variables (10 minutes)
Challenge 1: Control the Robot’s Speed
Activity:
Students will write code to make the robot move forward at different speeds by changing the value of a speed variable.
Instructions:
- Write code that allows the robot to move forward at a set speed for 3 seconds and then stop.
Expected Code:
#include <M3DGo.h>
int speed = 50; // Set the speed to 50%
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
go.forward(speed); // Move forward at 50% speed
go.delay(3000); // Move for 3 seconds
go.stop(); // Stop the robot
}
Challenge 2: Adjust the Robot’s Speed
Activity:
Students will change the value of the speed variable to observe how the robot’s speed changes.
Instructions:
- Modify the previous code to move the robot at 30%, 60%, and 100% speed for 2 seconds each. Ask them to predict how fast the robot will go at each speed and then observe the actual speed.
Expected Code:
#include <M3DGo.h>
int speed = 30; // Start with 30% speed
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
go.forward(speed); // Move at 30% speed
go.delay(2000); // Move for 2 seconds
go.stop();
go.delay(1000); // Wait 1 second
speed = 60; // Now set the speed to 60%
go.forward(speed); // Move at 60% speed
go.delay(2000); // Move for 2 seconds
go.stop();
go.delay(1000); // Wait 1 second
speed = 100; // Now set the speed to 100%
go.forward(speed); // Move at 100% speed
go.delay(2000); // Move for 2 seconds
go.stop(); // Stop the robot
}
Challenge 3: Rotate the Robot
Activity:
Students will control the robot’s rotation by creating a variable for rotation speed.
Instructions:
- Create a variable for rotation speed, set it to a value between 30% and 100%, and make the robot rotate clockwise for 2 seconds.
Expected Code:
#include <M3DGo.h>
int rotationSpeed = 60; // Set the rotation speed to 60%
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
go.spinClockwise(rotationSpeed); // Rotate clockwise at 60% speed
go.delay(2000); // Rotate for 2 seconds
go.stop(); // Stop the robot
}
Challenge 4: Move and Rotate
Activity:
Students will combine movement and rotation, using variables to control both speeds.
Instructions:
- Create two variables, one for forward speed and one for rotation speed. Make the robot move forward, then rotate, then stop.
Expected Code:
#include <M3DGo.h>
int forwardSpeed = 50; // Set the forward speed to 50%
int rotationSpeed = 40; // Set the rotation speed to 40%
void setup() {
go.begin(); // Initialize the robot
}
void loop() {
go.forward(forwardSpeed); // Move forward at 50% speed
go.delay(2000); // Move for 2 seconds
go.stop(); // Stop the robot
go.delay(1000); // Wait 1 second
go.spinClockwise(rotationSpeed); // Rotate clockwise at 40% speed
go.delay(2000); // Rotate for 2 seconds
go.stop(); // Stop the robot
}
Conclusion and Reflection (5 minutes)
Activity:
End the session by reflecting on what students learned about variables and how they impacted the robot’s speed and rotation.
Instructions:
- Ask students how they felt changing the speed of the robot using variables.
- Reflect on how easy it was to modify the robot’s behavior by adjusting a single variable.
Ask:
“What else do you think we could control using variables? What other parts of the robot’s behavior could we make more flexible with variables?”
Teacher’s Role:
- Lead the reflection discussion, encouraging students to think about how variables simplified controlling the robot.
- Praise their efforts in experimenting with the robot’s speed and rotation using code.
Learning Outcome:
By the end of this session, students will have a basic understanding of how to use variables to control the robot’s movement. They will have experimented with speed and rotation, learning how to modify these values to affect the robot’s behavior, reinforcing their understanding of variables in coding.