In this project you will turn your microbit into a toothbrush timer! Dentists recommend that you brush your teeth for 2 minutes so we will program our microbits to count 2 minutes and tell us how much time has elapsed and when to switch from your top teeth to your bottom teeth!
Go to the Makecode.com Microbit website using the link below and click on the 'New Project' button underneath the 'My Projects' heading.
Install the micro:bit app on your iPad or tablet.
Open the app, tap 'Create code' and then create a new project.
We're going to count to 2 minutes (or 120 seconds) so we need a variable to store the seconds as they tick by.
Create a variable called 'seconds'.
In the Variables toolbox, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
To start the toothbrush timer you will need to press the A button. So we will show an arrow pointing left (west) at the start. This will point at the A button.
You will find these blocks in the Advanced > Images toolbox.
Note: Change the arrow image to West (North is displayed in the code below).
images.arrowImage(ArrowNames.West).showImage(0)
Next we will program the A button to set the 'seconds' variable to 0 and then count to 120.
We do this by using a 'while' loop block. While the 'seconds' variable is less than or equal to 120, pause 1 second and then add 1 to the 'seconds' variable. This will keep adding 1 to the 'seconds' variable until it reaches 120.
input.onButtonPressed(Button.A, function () { seconds = 0 while (seconds <= 120) { basic.pause(1000) seconds += 1 } })
We're going to check how many seconds have passed and display a graph using the LEDs to show how many have.
To do this we first need to add an 'if then else' block inside the 'forever' block. We then use the plus button on the 'if then else' block to set it up like below:
basic.forever(function () { if (true) { } else if (false) { } else if (false) { } else if (false) { } })