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 and the length of string blocks from CONTROLS > 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:
- Make sure your board's COM port is selected from the device menu drop down.
- Click “Load to RAM”
- After your program loads, your terminal will display:
- Click in your terminal, and type your name and press the enter/return key when you are finished.
You won’t be able to use the delete/backspace key, so if you make a mistake, just reload your program and start over. Your program will then display how many letters long the name is that you typed in.
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:
Here is the if...do block, found in the CONTROL category:
The if...do block only works if you attach a block that makes a comparison. Here is the the compare values block from the OPERATORS > NUMBERS category:
To use this block, insert two values, and select a comparison to make between them from the dropdown 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 a the list of relational operators in the compare values drop down menu:
= Equal to
≠ Not equal to
> Greater than
≥ Greater than or equal to
< Less than
≤ Less 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 does an action if the condition is true, but the if...do block can be changed.
The if...do block is a special 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.
- Add more blocks to your program until it looks like this:
- Load your program to RAM.
- When the terminal opens, click into 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 if 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 your own program that gives a different message depending on what you enter, such as your age, what month you were born, or your favorite color.