2 3 Variables Senior Cde Blockly
2 3 Variables Senior Cde Blockly
3: Variables
You might know what variables are in math or science, but what do they do in
programming? This lesson will answer that question and show you how to use them.
Step 1
Getting Started
Make sure that your drone is connected with Blockly before starting the lesson! If
you are unsure of how to connect, go back to lesson 1.1 to review!
Step 2
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 experience
points. With your CoDrone EDU, you might use them to store the value of a sensor,
the speed of your drone, or maybe how far it will go.
Variables are especially beneficial if this value appears multiple times in your
program. Take a look at the following square code. What would you have to do
to make the square bigger? How can a variable solve this problem?
Step 3
Create a Variable
For every new variable you create, you will see a set of blocks created in the
“Variable” menu for you to use.
Step 4
Initializing a Variable
To initialize a variable means to assign the variable to a value. In this course, you
will normally set variables to a number, but there are other variable types as well
that you will learn about in a future lesson. Drag the set block with your variable
to the workspace.
Set the variable “x” to a number by accessing the “Math” menu and dragging in
the number block.
Then, change the 0 value to any number
Step 5
Access the Variable
When you need to use the value that is stored in the variable, you can use the
block with the variable name. In the example below, we are setting a new
variable “y” to 5.
Step 6
A good tool to know for debugging variable values is the print block. The print
block can be found inside the “Input/Output” menu.
Connect a variable or any block that returns a value to see the value in the
console window. Add a print block to your program and connect it to the block
that retrieves the “y” value.
Step 7
Now that you know the basics of using variables, let’s use variables to improve
the square program. Open or create a program that will fly CoDrone EDU in a
square shape. Let’s use the example from Step 2.
Run your code and observe its behavior. Then, create a new variable “seconds”
and set it to 1 at the top of your square program.
Find every part in your code that uses the number 1 to program a flight duration.
Now that you have a variable, you can replace each of these
duration parameters with the variable block for seconds. Drag and drop the
variable block directly into the seconds parameter for each block.
Run the code again and notice that it runs the exact same way as before!
Step 8
What would you do to make the square bigger? In the original program without
variables, you would need to change every move() block. Now, you only need to
change the variable value, and it will be applied for the entire program. Make
the square bigger by changing the value to 2. Make sure to clear a space that’s
large enough for your new flight path.
Step 9
Spiraling Square
To change a variable value in the middle of a program without user input, you
can use the change by block. This block will increase or decrease the value of
the variable by the number you input. For example, this program increases the
length of each side by 0.5 seconds for each side of the square. Draw the flight
path on paper. What does it look like?
Step 10
Challenge
Step 11
Lesson Complete
Let’s review what you learned about variables so far! You can use variables to
hold values for the CoDrone EDU to use in the program. In the activities above,
you got to see how to save values in a variable and use them with other blocks.