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.