Variable Blocks

Variables are one of the most important concepts you'll learn as you get into programming. You can think of a variable as a container or a box that holds information. BlocklyProp has two kinds of variables: string and integer (number). 

  • String variables hold text.
  • Integer variables hold positive or negative whole numbers. 

Although the Propeller microcontroller can work with all numbers, there are two important things to remember.

  • BlocklyProp does not support decimal points (known as a floating point). Keep this in mind if you plan on doing any division with numbers or variables.
  • You cannot store text in an integer container, and you cannot store a number in a string container.

The first program that you wrote told the Propeller microcontroller to send “Hello World” to the Terminal on your computer screen. Now you will use the Terminal to learn a little bit about how variables work.

Using the Variable Blocks

There are two variable blocks in the VARIABLES category. The first is the set variable block:

Use this block to store a value in a variable, by attaching a value block to it.

Then, the use variable block can be used in place of a value block in your program:

The item variable is a default included with BlocklyProp, but you can make your own. Let’s try it!

  • Under the VARIABLES menu, click the New variable button.

  • In the window that opens, type the name "myNumber" and then click OK.

Now, a use variable block for myNumber will appear in the VARIABLES menu.

Each variable you create in this project will be listed here. Each name you create must be unique to that project.  See About Naming Things for the rules.

The Variables menu will only show one set variable block, and its name may change as you create variables. But, that’s okay.

  • Place the set variable block in your workspace.
  • Choose myNumber from the block’s drop-down menu.

  • Attach a number value block from the VALUES category.

  • Now, create another variable and name it “myText”
  • Place a text string block from the VALUES category to the workspace and attach it to a set variable block for myText.

  • Change “Hello” to any text that you would like.

A series of characters that make up text is called a “string”. It is a good idea to keep your strings short. Some blocks limit the number of characters per string to 128.

Now, to see what is inside of your variables, we will print them to the Terminal. To set up the Terminal:

  • Drag a Terminal clear screen block from COMMUNICATE > TERMINAL and attach it to the end of your program.  
  • Attach a Terminal print text and a Terminal print number block to your program.  It should now look something like this (your variable text and number may differ):

  • Right-click the “Hello” and "0" blocks inside of the Terminal print text and Terminal print number blocks and delete them.
  • Click on the VARIABLES menu on the left to find the use variable blocks for the variables you created:

  • Place them in the two Terminal print blocks as shown below.  
  • Check the “then a new line” boxes in both Terminal print blocks:

  • Make sure that your BlocklyProp Client is connected, and that you have selected your COM port from the device menu.  
  • From the Run menu, select “Run once (load code to RAM)”.

Once your program has loaded, the Terminal will open and, after a brief connection message, it will display your variables.

Congratulations!  You are starting to learn how to create your own programs with BlocklyProp!

  • Save your program by clicking “Save” in the upper right-hand corner of your screen.

Did You Know?

All of the variables in Blockly are global.  A global variable is one that when set in one part of your program, can be used in a different part of your program as well. This works in two different ways. A different part of your program can 1) set the value of a variable, and 2) retrieve the value of the variable.

Try This

  • Click the drop-down arrow next to the “Save” button and select “Save as”.
  • Enter a new name for your program.  We will use “Math with Variables”.
  • Drag the number value block away from the first block in your program.

  • Attach a math operation block from the OPERATORS > NUMBERS  category to the set variable block.
  • Place the number value block into the math operation block as below:

  • Add a new number value block into the second space of the math operation block. Type in any number.

  • Load your program to RAM and rerun it.

Once your program has loaded, the Terminal will open and, after a brief connection message, it will display the new values stored in your variables.

Your program took the two values you placed in the math operation block and added them together before storing them in the variable “myNumber”. It displayed 50 because 42 + 8 = 50.

Your Turn

You may have noticed that the “+” sign in the math operation block is a drop-down menu.  You can choose other math operations as well.  Try each of the following ideas in your program:

  • Change the text and number values.
  • Try different mathematical operations, such a “-”, “x”, and “/”.
  • Use the drop-down menu on the Terminal print number block to print the number in hexadecimal or binary.
  • Try the random block from the OPERATORS  > NUMBERS category.
  • Try the length of string block from the OPERATORS > STRINGS category.