In the last tutorial, you learned how to send text and numbers to the terminal. Now you will use the terminal to send information to the Propeller to make a program that is more interactive. If a microcontroller is able to 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.
- Open a new Propeller C project in BlocklyProp.
- 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 typed in your name, 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 dropdown menu, you’ll need it soon.
- Add a pause block from the CONTROL category. It should default to 1000 ms (which is 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 BlocklyPropClient is running and connected.
- Select your board from the device menu.
- In the Run menu, select “Load 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.
This time, instead of collecting your name (or other text), you will collect a number value using the terminal. When you collect numbers, you can then do something 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 “Load 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 another 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:
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 it is connected to.
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?
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 is 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 a string of characters is stored in the Propeller’s 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.
- Build a Mad Libs® style “fill in the blank” game. Ask a friend to enter a noun or verb (or other part of speech), and then print the word they entered into a sentence. You can build a program that asks for a number of different words, and then print them out as part of a story.
(The real Mad Libs is an awesome game and a registered trademark of Penguin Random House LLC.)