5. Timer and stopwatch

Timer and Stopwatch

From now on, you can use the Micro:Bit as a Timer or Stopwatch.

Press A to start the time, B to pause the stopwatch, and A and B simultaneously to set your time to 0.

You will learn:

• How to use variables that are ‘true’ or ‘false’.
• How to use loops.

Log in before starting the exercise.

Click here

ORIENTATION

Take a look at the example and consider the following questions:

  • What should I do?
  • What are the goals?
  • What are the expectations?
  • What can I already do?
  • What do I still have to discover?

PREPARATION

View the steps at 'perform'. Then answer the following questions:

  • Which steps should I follow?
  • Which steps can I follow?
  • How much time do I need?
  • What tools do I need?

PREFORM

Do the steps one by one. Do you not remember how to do it? View the hints or download the solution.

Stopwatch

Step 1

TIME INDICATION

We will make a stopwatch that will start with 0 and that will keep track of time.
First, we will make another variable. The variable will need to keep track of the time. Give it a suitable name.
We have to be able to see how much time has passed the entire time. Can you take care of that?

Step 2

STEP 2 KEEPING TRACK OF THE TIME

The stopwatch has to start with one push of the A-button. From that moment on, we have to be able to keep track of the time. With every second we will need to raise our variable.
We would use the block ’repeat’ in Scratch. The block with same function in Micro:Bit is:

while true

 

You will see how the code works once you test it. However, something weird happens after 10 seconds. What is going on?

toelichting

Everything works well up to 10 seconds. However, it takes more time for the Micro:Bit to show numbers with two figures. This is why not all of the numbers are shown. You will need a bigger screen for that...

Step 3

Stop the stopwatch

The stopwatch has to stop after pushing the B-button. Can you change the code?
There are a couple of ways to do this. Either way, you will have to change ‘true’ by using one or multiple blocks. We will explain how to do this using a variable, but there is another way.
Make a variable named ‘stopwatch’. We will put the stopwatch on ‘true’ if the time has to increase.
We will put the stopwatch on ‘false’ if the time has to stop. Can you change the variable at the right places?
Use the following blocks:

set stopwatch

true false

Can you find both of them?

Assignment:
• If you press A, ‘true’ has to be set up on the stopwatch.
• While the stopwatch remains true, the program that keeps track of the time will continue to run.
• If you press B, ‘false’ has to be set up on the stopwatch.

uitdaging

Can you set the stopwatch to 0 when A and B are pressed simultaneously?

Timer

Step 1

Changing the code

We will make a timer now. This is not as difficult as making the stopwatch! A timer will count down until it reaches 0.
You will need the following buttons:

  • A button that changes the time by 1. Use the B-button for this purpose. Whenever you press that button, a second will be added.
    • A button that begins to count down. Time changes by -1. Use the A-button for this purpose.

 

Will you change the code of the stopwatch?

 

Step 2

No negative time

The timer does not entirely do the things we want it to do. You want the timer to stop once the time hits 0. This is currently not the case.
Can you change the code so that the timer stops at 0?


You can set up the Micro:Bit so that it only shows the positive time. For the other cases, it has to show 0

uitdaging

a)Will you make sure that a cool animation pops up once the time hits 0?
b)Can you change the code so that you can change the time to 0 by pressing A and B simultaneously?

 

Well done, you can now use the Micro:Bit during gym

REFLECT

Look back at the project you just made. Ask yourself the following questions:

Does the program work as I expected? Why / why not?
What works well?
What works less well?
How can I approach it differently?

Download

Click on the link below to download a sample solution.