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).
Although the Propeller microcontroller can work with all numbers, there are two important things to remember.
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.
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!
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 [1] 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.
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:
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!
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.
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.
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:
Links
[1] http://learn.parallax.com/support/reference/propeller-blocklyprop-block-reference/about-naming-things