[RL43] Grade 4 - Session 3

Session 3: Introduction to Variables in Coding

Session Architect: Muhammad Bilal
Duration: 50-60 minutes


Equipment Required:

  • M3D Robotics kit.
  • Laptops with Scratch pre-installed.
  • Projector to display Scratch interface for demonstration.
  • Paper and pencils for students to document their discoveries and ideas.

Learning Objectives:

  • Understand the concept of variables in coding and how they can store information.
  • Learn to use pre-existing variables in Scratch (e.g., “answer”, “timer”, “position”).
  • Create custom variables in Scratch to store and change values.
  • Apply variables in real-time to track data (e.g., robot’s distance traveled, time elapsed).
  • Experiment with adjusting variable values to observe changes in robot behavior.

Resources:

Scratch code for the session: Grade 4 Session 2.sb3


1. Introduction to Variables and Constants (10 minutes)

Activity:
Start by introducing the concept of variables alongside constants, using real-life examples to make it relatable for the students. Help them understand that while variables can change, constants stay the same.

Objective:
Teach the concept of variables through a simple and engaging physical activity where a student’s steps are tracked as a changing value (variable).


Instructions:

  1. Set the Stage:
    • Start by calling a student to the front of the class.
    • On the board, write: “Steps taken by [Student’s Name]: 0”
      Explain to the class:

“Right now, our friend [Student’s Name] hasn’t taken any steps, so the number is 0. This is like a variable. It stores a value, which in this case is the number of steps, but that value can change.”

  1. Demonstrate Changing the Variable:
    • Now, write on the board: “Take 3 steps.”
    • Ask the student to take 3 steps. After they take the steps, update the number on the board to 3:

Steps taken by [Student’s Name]: 3

You may explain:

"Look, we’ve changed the number of steps [Student’s Name] has taken. It went from 0 to 3! That’s how a variable works. It can hold different values over time.”

  1. Modify the Variable:
    • Erase the command “Take 3 steps” and write a new one: “Take 4 steps.”
    • The student takes 4 more steps. Update the number on the board to:
      “Steps taken by [Student’s Name]: 7” (sum of the previous steps and new steps).
    • Now ask the class: “How many steps has [Student’s Name] taken in total?”
      They should answer “7,” and you can point out:

“How do we know that? Because we kept track of the steps using our variable ‘Steps taken by [Student’s Name].’ Every time the number changes, we update it. But the value is always changing as more steps are taken.”

  1. Explain the Concept of Variables:
    • Point to the updated number on the board and say:

“This is exactly what a variable does in Scratch. It keeps track of a value that can change, just like we did with the number of steps [Student’s Name] took. Every time something happens, the variable updates.”

  1. Connect to the Robot:
    • Finish by saying:

“Our robot can also keep track of how many steps it takes, just like we did with [Student’s Name]. Let’s try doing this in Scratch, where we’ll create a variable to track the robot’s steps!”


Teacher’s Role:

  • Guide the student through taking steps and make sure the class follows along with the changes on the board.
  • Emphasize how the number of steps (variable) changes based on the actions performed (steps taken).
  • Encourage the class to think about other examples where variables might be used (e.g., keeping track of scores in a game).
  • Lead them to the idea that variables in Scratch work just like keeping track of steps—they change as actions happen.

2. Exploring Pre-Existing Variables in Scratch (10 minutes)

Activity:
You can say:
“Let’s start by looking at some variables that Scratch already has.”
Introduce students to variables that already exist in Scratch, like “answer,” “timer,” “position x/y”.

Instructions:
Demonstrate on the projector how certain blocks in Scratch already store and use variables. For example:

  1. “Ask ___ and wait” stores the response in a variable called “answer.”

Let students experiment with these pre-existing variables:

“Now, let’s ask the robot a question and see how the ‘answer’ variable works.”

Show them how they can check the value stored in the variable by dragging it from the list of blocks and clicking on it.

  1. “Timer” keeps track of how many seconds have passed since the program started.

image


Show them how to use these variables. You may say:

“Look at the timer here—it tells us how long it’s been since we clicked the green flag. That’s a variable that stores the time!”

  1. “Position x” and “position y” tell the horizontal and vertical position of the sprite.

Note that the students don’t confuse the position of the actual robot with the position of the sprite. These code blocks tell us the position of the sprite inside the gamepad.

Teacher’s Role:
Explain each pre-existing variable with simple examples and encourage students to test them out. Help them understand how variables help store important information that changes as the robot moves or as they interact with the code.


3. Creating and Using Custom Variables (15 minutes)

Activity:
Students will learn how to create their own variables in Scratch, store information in them, and adjust values.

Instructions:

  1. Introduce the “Make a Variable” option in Scratch. Show them how to create a new variable:

“Let’s say we want to keep track of how many steps our robot takes. We can make a new variable called ‘Steps Taken’ and store that information.”


Highlight the importance of choosing the right name for the variable so that when the student looks at it, it can remind them of the value that is stored in it.

After the variable is created, it will look like this:

Encourage them to identify how all variables are “pill-shaped” blocks. This will help them identify variables from other blocks quickly when creating larger code blocks.

  1. Guide them in creating a few custom variables, such as:

    • Robot’s Name: Store the robot’s name.
    • Distance Traveled: Store how far the robot has moved.
    • Number of Goals Scored: Store the number of goals if the robot is playing a game.
  2. Demonstrate how to use variable blocks like:

Teacher’s Role:
Help students create their own variables and guide them in understanding how the robot uses these variables to store changing information. Help them understand the difference between changing the value and incrementing the value of the variable.


4. Experimenting with Variables in Robot Movement (15 minutes)

Activity:
Students will apply what they’ve learned by creating variables to track the robot’s movement in real time.

Instructions:

  • Ask students to create a variable called “Steps Taken” and have it increase each time the robot moves:

“Every time the robot takes a step, we’ll increase the value of ‘Steps Taken’ by 1. Let’s see how far it travels!”

  • Let them program the robot to move forward, and after each movement, they’ll use the “Change [Steps Taken] by 1” block to update the variable.

  • Students can also track time using the “Timer” variable to see how long it takes for the robot to reach a destination.

  • Encourage students to build larger coding blocks where they store different values e.g. steps taken, left turns taken, right turns taken etc.

Ask them to check if the variable is showing the correct number of steps when the movement ends by using the “Show Variable” block:

Teacher’s Role:
Encourage students to observe how the robot’s behavior changes when they adjust the variables. Guide them to experiment with combining variables like distance and time to see how fast the robot moves over a given distance.


5. Showing Emotions and Messages

Activity:
Students will connect the display attachment to their robot and use the “Show Text” and “Express” blocks to display emotions and write any message they want (e.g., their name, “Hello!”) on the robot’s screen.


Instructions:
You may lead this segment as:

"Great job, everyone! You’ve all worked so hard learning about variables today, and now it’s time for some fun! We’re going to let our robots show off their emotions and even say something on their screen.

Let’s see how creative you can get with making your robot come alive with expressions and text. Let’s start by connecting the display and get ready to make our robots talk and express how they feel!"

  1. Connect the Display:

    • Guide students to attach the display to the robot.
    • You might say:
      “Now that we’ve learned how variables work, let’s have some fun! We’re going to make our robots show emotions and write messages on their screens!”
  2. Use the “Express” Block:

“Let’s make the robot smile! Or maybe it’s feeling excited—try out a surprised face!”

  1. Use the “Show Text” Block:

“Now, you can make your robot say something! Write ‘Hello!’ or even your name on the robot’s screen.”

  1. Experiment:
    • Encourage students to mix and match the “Express” and “Show Text” blocks to create fun combinations, like a happy face and the message “Let’s Dance!”
      “See how your robot can express different emotions and talk to us with text!”

Teacher’s Role:

  • Help students connect the display and guide them through choosing expressions and writing text.
  • Encourage creativity by letting students experiment with different combinations of emotions and messages.
  • Celebrate their creations by having them share their robot’s expressions and messages with the class.

6. Conclusion and Wrap-Up (5 minutes)

Activity:
End the session with a discussion about how variables help us store and use information in real life and in coding.

Instructions:

  • Recap the key points about variables and how they store information.
  • Ask students to share one example of a variable they created today and how it changed during the activity.
  • Ask them what they enjoyed most during the session and which part was most challenging.

Teacher’s Role:
Facilitate the discussion, encouraging students to reflect on how they used variables to track robot movements and other information. Help them connect this lesson to the idea of storing and using information in everyday situations.


Learning Outcome:
By the end of the session, students will have a solid understanding of how variables work in coding, how to create and modify them, and how they can be applied to real-time tasks like tracking distance, time, or other data related to the robot’s behavior. With the addition of a fun activity at the end, students will be able to relax and have fun with the robot by making it come alive with expressions and messages.