In the previous tutorial, you learned how to send text and numbers to the Terminal. Now you will use the Terminal to send information to the Propeller microcontroller and to make a program that is more interactive. If a microcontroller can respond and react to either a person or its environment, it can be much more useful.
Let’s write a program that asks you to type in your name, and then repeats it back to you by printing it to the terminal.
Your program will look like this:
To build the program:
You should see this:
This time, instead of collecting your name (or other text), you will obtain a numerical value using the Terminal. When you obtain numbers, you can then do something useful with those numbers. For instance, you can collect two numbers and add them together - using the Activity Board WX like a calculator. Let’s try it:
In the second half of the program you just built, it required many blocks just to print the line "Adding your numbers together: 42 plus 55 equals 97".
There is a block that can be used to combine a bunch of different strings, numbers, and variables:
This block is the Terminal print multiple block. You will find this block in the TERMINAL > COMMUNICATE menu.
Let's try it out!
When you type into the Terminal, your computer sends information back to your Propeller board. By using the Terminal receive text blocks, the Propeller microcontroller knows that it is supposed to stop and wait for information to come to it from the computer to which it is connected.
When you use the Terminal receive text blocks, the Propeller listens for each character you type, and stores them in a special hidden variable called a buffer until it receives the character sent when you press your return key. When the Propeller sees that you sent a return key character, it takes the information collected in the buffer and places it in your variable.
Did you know that you can add things like tabs or carriage returns (new lines) to the strings you are sending to the terminal? There is a special value block called the character value block, found under the VALUES category. When you use it with the Terminal print multiple block, you can add special characters to the information you are sending:
Each character takes up 8 bits of information. Each bit can only be a 1 (one) or a 0 (zero). Each character has its own 8-bit code, and 8 bits are called a byte. The set of codes for characters is called ASCII. For instance, the ASCII code for the character ‘Z’ is 01011010 in binary, which is 90 in decimal. The ASCII code for the return key is 00001101, or 13 in decimal. See the ASCII Table [2] for a list of more characters and their code numbers in decimal.
When the Propeller stores a string of characters in its memory, it always takes up one extra character of space. For example, if you store the 11 character string “Hello World” in a variable, it takes up 12 bytes - the last byte is a zero (00000000) so that the Propeller knows where the end of the string is.
(The real Mad Libs [3] is an awesome game and a registered trademark of Penguin Random House LLC.)
Links
[1] https://solo.parallax.com/
[2] http://learn.parallax.com/ascii
[3] http://www.madlibs.com/