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.
Test Code
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.
- In BlocklyProp Solo, create a new project for your board type.
- Choose a name for your project. As an example, we will use “Get Text From Terminal”.
- The program will first ask you to type in your name. Then, after you have done that, it will wait 1 second before repeating back to you what you typed.
Your program will look like this:
To build the program:
- Start by dragging a Terminal print text block to the workspace and change the text to “Type in your name: ”.
- Next, add a Terminal receive text block from COMMUNICATE > TERMINAL. Pay attention to the variable name in the drop-down menu; you’ll need it soon.
- Add a pause block from the CONTROL category. It should default to 1000 ms (which is equal to 1 second), so there is no need to change anything in this block.
- Add a Terminal new line block from COMMUNICATE > TERMINAL.
- Add another Terminal print text block.
- Drag the text string block (the block that says “Hello”) out of the Terminal print text block and into the Trash Can, then replace it with a use variable block. Make sure the variable name is the same as the one used in the Terminal receive text block above.
- Check the “then a new line” box.
- Save your program.
- Make sure that you have plugged in and turned on your Activity Board and that the BlocklyProp Client is running and connected.
- Select your board from the device menu.
- In the Run menu, select Run once (load code to RAM).
You should see this:
- Click in the Terminal after the line “Type in your name:”. You will see a blinking cursor.
- Type in your name and press the enter key.
- After 1 second, the Propeller will print your name on the next line.
Try 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:
- Open a new program and give it a name. We used “Add Two Numbers From the Terminal”.
- Build the program shown:
- Save your program.
- In the Run menu, click Run once (load code to RAM).
- After your program loads, the Terminal should pop up. After a brief connection message, the screen will clear, and this will display:
- Click in the Terminal after the text “Type in a number:” and type a number, then press enter.
- Another phrase asking for a second number will appear. Click the Terminal after the text and type in another number, then press enter.
- After a 1 second pause, the Terminal will display something like this:
Blocks that can change
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!
- Grab the first Terminal print text block after the pause block and drag it away to the right:
- Attach a Terminal print multiple block after the pause block:
- Click the gear icon on the Terminal print multiple block:
- You can change the Terminal print multiple block by dragging the smaller blocks into and out of the container block:
- Fill the container block with the following blocks as shown:
- Click the gear icon to close the mutator window.
- Drag each of the values from the original Terminal print multiple and Terminal print multiple blocks into the new Terminal print multiple block.
- Delete the old, unused Terminal print blocks:
How it Works
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.
Pro Tip
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:
Did You Know?
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 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.
Your Turn
- Build a Mad Libs® style fill in the blank game. Ask a friend to enter a noun or verb (or another part of speech), and then print the word they entered into a sentence. You can build a program that asks for several different words and then prints them out as part of a story.
(The real Mad Libs is an awesome game and a registered trademark of Penguin Random House LLC.)