LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Computer – micro:bit Talk

Computer – micro:bit Talk

What it’s about

This tutorial introduces some ways to communicate with your micro:bit module through a computer.  It’s a lot like texting, but you’ll be exchanging messages with your micro:bit instead of another person.  The app for this is called the serial monitor, and you already experimented with it in Use the Serial Monitor. 

Before you start

You will need:

  • A micro:bit module, alone or on a cyber:bot
  • USB A to MicroB cable
  • Computer with at least one USB A port (or a C to A adapter)
  • An up-to-date Chrome or Microsoft Edge browser.

Complete these tutorials first:

  • Get started with micro:bit and MakeCode
  • Making micro:bit programs

After you finish

You will be able to make Makecode projects that make your micro:bit receive, process, and respond to text and numeric values you type into a serial monitor.  Along the way, you will build basic skills for tutorials on these subjects:

  • Dictionary Primer
  • Radio Basics
  • …and many more

Meet the Terminal

Most of the Cybersecurity tutorials will use the serial monitor to interact with a micro:bit while its project is running.  For example, if the micro:bit picks up any radio messages, it will be able to display them quickly on the serial monitor.  For the cyber:bot, it is also useful for testing sensor circuits before relying on them for navigation.

Why is it called a terminal?

The serial monitor is a kind of terminal. In computing, a terminal is “A combination of a keyboard and output device (such as a video display unit) by which data can be entered into or output from a computer or electronic communications system” according to Merriam-Webster.

Large computers with numerous “terminals” connected to them used to be common in business and academics.  Each terminal had a monitor and keyboard, and sometimes other devices. 

Image source: “DEC VT100” by Gorthmog is licensed under CC BY-SA 4.0

With the advent of more sophisticated computers and networks, terminal emulator apps like the Windows command prompt became common.  

The “terminal” we use is also considered a computer terminal emulator app, even though it is communicating with a microcontroller.

Hardware terminals are still in use today.  For example, an ATM (Automated Teller Machine) is a terminal that’s connected to the bank’s system.  At the ATM, a user can access their bank account and interact with a bank to withdraw or deposit money, check account balances, etc.

Image source: “ATM_750x1300.jpg” by Rfc1394 is released into Public Domain

Projects that Print to Terminal

Recall from Using the Serial Monitor that you can flash a project to the micro:bit that prints a message, and open the Serial monitor to see that message. Makecode projects can also use serial write to display messages and values as the projects run. This can be helpful for displaying longer messages, and also for displaying what’s happening to values at certain points in your project. And, since you flash the project to the micro:bit, your code travels with your device.

In this activity, you will print messages, values, and combinations of both in the terminal.

Example project: print_to_terminal

  • Connect your micro:bit module to your computer with its USB cable.
  • In a browser, go to makecode.microbit.org to open the micro:bit Makecode Editor.
  • Enter and name it print_to_terminal.
    (See Save & Edit Projects.) 
  • Click Download.
    (See Flash Projects with MakeCode Editor.)

  • Navigate to a new browser and open Google Chromelabs Serial Terminal
    (See Use the Serial Monitor.)
  • Connect to your micro:bit using the connect button.
  • Verify that the Hello Terminal! message is displayed.

  • Want to see it again or nothing showed up?? Hit the reset button on the back of the micro:bit.

Pressing and releasing the micro:bit module’s Reset button, (which is next to its USB connector) restarts the project that’s currently flashed. After that, the micro:bit runs the project again and whenever it executes a serial write block, the result appears in the terminal. In this case, it’s the Hello Terminal! message. 

Try This: Print Something Different

What if you want to display a different message in the online editor? Try modifying the text inside the serial write block.

  • Change project’s name from print_to_terminal to print_to_terminal_try_this.
  • Replace the Hello Terminal! with your own new message.
  • Click the Download button.
  • Check the results in the serial monitor.

Did You Know?

The micro:bit’s serial write block is completely different from the show blocks and its methods. One of those outputs is not as fast as the other. The show methods are for the 5×5 LED grid, and each block has to display each character long enough for a human to read it. In contrast, the micro:bit serial write block can send characters very rapidly, with each character taking only about 1/10,000th of a second to transmit.

Example project: display Integer Values

You might have noticed that serial write can display values stored by variables, along with strings of characters. That same functionality is also available when you add serial write blocks to projects.

  • Enter and name the project print_value_to_terminal. 
  • Click the Download button.

  • Check the results in the serial monitor.
  • Verify that the terminal displays Hello Terminal followed by value = 10 on the next line.
  • If there are random missing letters ignore them, it’s not your program’s fault it’s the terminals.

Print Values to Terminal in a Loop

Let’s try a countdown timer in the terminal using a while loop. The while block repeatedly executes blocks that are indented immediately below it. The loop repeats “while” its condition is true. In this program, the while loop repeats while the value variable is greater than or equal to 0.

Example project: print_looped_terminal_values

  • Enter and name the project print_looped_terminal_values. 
  • Click the Download button.

  • Navigate to the serial monitor, if it’s not open go to Google Chromelabs Serial Terminal.
  • Click Download.
  • Verify that the terminal displays value = 5, value = 4, …value = 1, Blastoff!

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.

Input/Print for Applications

Now that your micro:bit projects can receive values from the terminal, let’s put those values to work in an app. This first example is a countdown timer that allows you to set the number of seconds in the countdown.

  • Enter and name the project countdown_timer. 
  • Click the Download button.

  • Navigate to the serial monitor, if it’s not open go to Google Chromelabs Serial Terminal.
  • Click to the right of the “Enter seconds to count down: “ prompt.
  • Type a number and press Enter.
  • The program will then prompt you with Enter message after countdown:. Type the message you want it to display, then press Enter.
  • Verify that the terminal counts down to zero from the starting value you gave it, and then displays your custom message.

How it Works

First, the project captures the text representation of a number you type into the terminal. This gets converted into an int value that the project can store and use for counting repetitions. This int value gets stored in the value variable.

The text you type in response to the message prompt gets stored as a string of characters in the message variable, which is a string.

Since the value variable now stores an int variable, it can be used to control how many times this while loop repeats:

After the while loop is done, your custom message gets displayed.

Try This: Calculator App

This is another way of using text and values from the terminal — a calculator app.

  • Enter and name the calculator project. 
  • Click the Download button.

  • Navigate to the serial monitor, if it’s not open go to Google Chromelabs Serial Terminal.
  • Test the calculator app, and verify that it works. Make sure to press Enter after each value and operator you type.

Your Turn: An Interval Timer

Interval timers repeatedly count down the same amount of time, and are used to control the amount of time one has to repeatedly run a sprint or swim a lap, including rest. They are also used in stretching after a workout so that each stretch is only done for a fixed amount of time.

  • Make the app ask for the seconds to countdown, and the number of reps.
  • Then, the interval timer will countdown the number of seconds you entered, and it will repeat that countdown the number of times you typed for reps.
  • After you get it working, put the routine in a while True loop so that it it starts over again, asking you for seconds and reps after the first set of intervals.

It helps to break this program into steps. Here is some pseudo-code that you can work from to build the program:

Outer loop always repeats

Display a string asking for the countdown seconds.
Get the countdown seconds and store in an int variable.    
Display a string asking for the reps.
Get the reps and store in an int variable.
while reps > 0
    copy countdown seconds to a temporary variable.
    Print start interval message
    while  temporary variable > 0
        Print temporary variable
        Wait a second
        Subtract 1 from temporary variable.
    Subtract 1 from reps
Print done with set

Hint: Make the indentation levels in the pseudo code match the indentation levels in your code.

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/computer-microbit-talk_makecode/
Links