After you've learned the basics of programming, you will begin connecting inputs, such as buttons or sensors, and outputs, such as screens and lights, to your Propeller Activity Board WX. You may want to write a program that makes a decision. For instance, you may want your program to turn on a light when you press a button.
In this program, you will use the Terminal receive text block located under COMMUNICATE > TERMINAL and the length of string block from OPERATORS > STRINGS to make a program that decides if an individual’s first name is shorter or longer than 7 letters.
- Open a new BlocklyProp Project.
- Build the following program:
- Select your board's COM port from the device menu drop-down.
- Click “Run once (load code to RAM)”
- After your program loads, the Terminal will display:
- Click in the Terminal, type your name, and press the enter/return key when you are finished.
You will not be able to use the delete/backspace key, so if you make a mistake, just reload your program and start over. Now, your program will display how many letters long the name is that you entered.
Pro Tip: Use Spaces!
Use spaces when typing text in your block to help make your program more readable in the Terminal. If you don’t add spaces to text that appears before and after a number that you are trying to print in the middle of a statement, it will look like this:
By adding spaces, the output is easier to read:
These extra spaces will print this in the terminal:
Below is the if...do block, found in the CONTROL category:
The if...do block only works if you attach another block that makes a comparison. For example, the compare values block from the OPERATORS > NUMBERS category as shown here:
To use this block, insert two values and select a comparison to make between them from the drop-down menu. The block evaluates the comparison statement and provides a value of 1 if the statement is true, or 0 if the statement is false.
Did You Know?
The = symbol is called a relational operator. Here is the list of relational operators in the compare values drop down menu:
= Equal to
≠ Not equal to
< Less than
≤ Less than or equal to
> Greater than
≥ Greater than or equal to
Sometimes, you may need to do one action if a condition is true, and a different action if it is false. In its original form, the if...do block only completes an action if the condition is true. You can, however, alter the if...do block.
The if...do block is a unique mutating block, meaning you can change how it works. Click on the blue gear icon in front of the word “if,” and a box will pop up for selecting options:
- Click on the blue gear to change the if...do block.
- Drag an else block into the if block:
- Click the blue gear icon again to shrink the options.
- Attach your newly modified if...do block to the end of your program.
- Remove the Terminal new line block and check the "then a new line" box.
- Add more blocks to your program until it looks like this:
- Load your program to RAM.
- When the Terminal opens, click on the Terminal and type your first name.
- Press enter/return.
- Observe what prints out in the terminal:
What if you want to test whether the name you entered is exactly 7 letters long, in addition to testing whether it is greater or less than 7 letters long? You can mutate the if...do block again to include an else if condition:
- Now, try building a program that gives a different message depending on what you enter, such as your age, what month you were born, or your favorite color.