a1tD00000087wSvIAI

Course: Programming with CoDrone Lite
3-Variables in CoDrone Lite

  • 4-7 grade
  • Beginner

Lesson Description:

Variables will be part of almost every program that a student makes to fly their CoDrone. These lessons will cover what variables are and how they work, and then students will create and run their own programs using variables. Students will set variables and manipulate them to make multi-sized shapes and patterns in flight.

Objective:  Explain what a variable is, how it works, and why it is used with a real-world model. Create and run a program with a variable in Snap. Modify the value of a variable in Snap. Update an existing program in Snap to allow for variables and their change in size.

image description

Lesson Modules


Teaching Tips:

  • At this point, students most likely have heard the word variable in their math and/or science classes. Ask them to figure out if a variable in coding would be the same thing.

 What is a variable? How is it used in programming? How were they able to use it in class?


Teaching Tips:

  • Call two students up to the front of the class to play a short trivia game against each other (examples: multiplication tables, state/country capitals, periodic elements, etc.). Do not visibly keep score, but after about five questions, ask the class who is winning. How is score kept in a digital game? (Answer: variables that store the information!) Bring up two more students and give them each a whiteboard and a marker, and then explain that they will be variables keeping track of the score. Ask the class how they will know who the students are keeping track of, how many points a correct answer should be given, and at what point the game will be over. After this is figured out, start the trivia game again, but remind the variables that they will need to add the appropriate points when a question is answered correctly. Play until there is a winner, and then split students up into groups of 5: 2 students answering questions, 2 students acting as variables, and 1 student answering questions. Roles can be switched if there is time! Occasionally gather top scores from the class and write them on the board. After 10-15 minutes, stop the activity.
  • Ask students how they think a variable works after going through the activity. What did the variables do when a point was scored, when a new player started, or when a new game began? How was the teacher able to gather the top scores? Where else in games are variables used?

Trivia game

What are variables?

Variables are a way to store information in a program, and they're used most often to store values. If you're playing a video game, there's a variable to store your score, another one for your lives, and maybe another one for time. With your CoDrone, you might use them to store the value of a sensor, the speed of your robot, or maybe how far it will go.

Variables are great if you have a lot of values in your program! For example, you might have a program with five different places where you set the speed. Without a variable, you would need to change all five numbers. With a variable, you would only need to change the variable's value.



Teaching Tips:


Now, let's make our own variable in Snap!

Ready to make your own variable? Go to the orange variables menu and click on the gray "Make a variable" block at the top. Snap! will then ask you to name the variable. This can be whatever you want --- we just named ours "testing". Once the variable is made, it will shows up as a block in the orange variables menu.

Wondering what the check mark next to the variable is for? That controls whether you can see the variable at the top righthand corner of your screen or not.


Setting variables

The "set to" block will set your variable to the value at the end of the block, which you can select from the drop-down menu. To set this value, you can:

  • Type in a number
  • Use another variable or a block from the green operators menu (your math teacher will love this menu!)

The block will then save your values into the variable.


Change variables

The "change by" block will add the value at the end of the block to your variable, which you can select from the drop-down menu. To set this value, you can:

  • Type in a number to change the value. If the number is positive it will increase the value of the variable, and if the number is negative it will decrease the value of the variable.
  • Use another variable or a block from the green operators menu

Put it all together

Ready to test variables in the wild? You're going to combine what you learned about variables with what you learned about starting blocks. Make a program that will set your variable to 5 when you press the green flag and add 1 to your variable every time you press the space key.

If you're not sure how to do this yet, here's the spoiler:

  • Under the "when green flag clicked" block, connect the "set to" block. Make sure you choose your variable from the drop-down menu and set the value to 5.
  • Under the "when key pressed" block, connect the "change to" block. Make sure you choose your variable from the drop-down menu and set the value to 1.


What can you do with variables?

Variables are a way for you to save a value so you can use it later on in your program. They are usually created and placed at the top of your program, right underneath the starting block.

Variables are great if you have a long or complex program that has a lot of values that you might need to change. Without variables, you would have to go back and change each value one by one, which leaves a lot of room for mistakes. With a variable, you can just go back up to its block at the top of the program, change the value, and then it's changed throughout the entire program!


How to change a variable's value

There might be times where you need to change one variable, but not the other. You can still do this! After you create your variable and have it in your program, go to the orange variables menu, and drag and drop the change by block where you need it. This will most likely be a block that has a variable in it. In our code, it is under the yellow wait secs blocks.

Next, select your variable in the drop down menu and type in the value that you want to change your variable by. You can also use the blocks in the green operators menu or another variable (if you have it). Just remember that if you want to have your variable's value decrease, you need to use a negative number.




Teaching Tips:


What is a variable and how is it used? Give an example of a variable and how that variable works.

How can you change the value of a variable in Snap?