Input Messages
You might have been wondering if there’s a way to type messages into the terminal and send them to the micro:bit. Well, there is — with the help of the read until block.
- Enter and name the project enter_a_message.
- Click the Download button.
- Check the results in the serial monitor.
(Click the Connect button if needed.) - If the prompt doesn’t immediately display, press/release the micro:bit module’s reset button.
- Make sure you have the Local echo box checked so you can see what your typing.
- Click to the right of the “Enter some text: “ prompt, and type “Hello World!” followed by the Enter key.
- Verify that the next line in the terminal displays: message = Hello World!
How it Works
The serial write block can send a prompt, like “Enter some text: “, then the read until block receives the characters you type, and returns them as a string. That string normally gets stored in a variable with a block like set message to ( serial read until () ). The text you type (up to when you press Enter) will get stored in the message variable, and Makecode will automatically set that variable’s type to string. For more on data types, see the Remember and Use Values page.
Try This: Input Your Name
Customize the example project to ask for and store your name.
- Change project’s name from enter_a_message to enter_a_message_try_this.
- change “Enter some text: “ to “What is your name? “.
- Click the Download button.
Your Turn: Expand the Project
- Now that you have a project that asks for your name, expand it to ask for, remember, and report your favorite color.
- Use a loop to make the project repeatedly ask for and display your name and favorite color indefinitely.
- Modify the loop so that it only runs three times.
Did you use your real name? Don’t worry, the micro:bit is not collecting your personal information! But do keep in mind that there are social aspects to cybersecurity, not just technical!
Input a Value
Let’s say you’re working on a project where you have to enter the cyber:bot robot’s left and right wheel speeds into the terminal. It might seem like all you’d have to do would be to type in numbers like 50 and 100. The problem is that those numbers are received as strings of characters “50” and “100” and then stored in string variables. Before your project can use those values to set robot wheel speeds, it needs to convert them from string to int values with the parse to number block.
- Enter and name the project enter_a_value.
- Click the Download button.
- Navigate to the serial monitor, if it’s not open go to Google Chromelabs Serial Terminal.
- Click the serial monitor to the right of the Enter a value: prompt.
- After it says Enter a value:, type “23” and press Enter.
- Verify that it displays value = 23.
How It Works
If you typed 23 in response to the prompt, the set text to (serial read until () ) block stored “23” in the text variable. This is a string of characters, not a usable number. The next block, set value to (parse to number (text)), converts the “23” string to the integer 23 and stores it in the value variable.
Think about this as two steps. First, the parse to number(text) block takes the text variable, which stores the “23” string, and returns the int value 23. That result gets copied to the int variable named value. Keep in mind that Makecode automatically sets the variable type to int.
Next, serial write line (“value = “, value) prints “value = . Then, the serial write block has to convert the int value 23 stored by value into the character string “23” before sending it to the terminal.
The most important point in this exercise is that you entered characters that the project was able to convert to values it can use to make calculations, control repetitions, and more.