Microbit-> against the clock

micro-bit-featured you have already discovered the microphone: bit, then learned how to use it and programmed a random game.

this time we will learn how to create a timer, to be able to challenge our friends through our micro: bit or all just achieve a normal timer.


  • simultaneous support on the 2 buttons back the timer to 0

  • support on the button starts timer

  • seconds parade on the LED display

  • support on the button B stop timer

step 1 : Configuration of your timer

the timer is reset to 0 when the buttons A and B are pressed together.


  • click on “ add an event ” and create a new event onPressAandB .


  • click on the tab “ library ” and “ Globals “, and create a new variable called time .


  • you want when the buttons A and B are pressed together, the variable time take the value 0. To do this, drag your variable time in your event onPressAandB .


  • click update and # number and enter 0 .


here is what your code should look like:


  • you must also display the time (variable time ). To do this, drag a block say , click the value and choose your variable time .


  • click on “ run ‘ to test your code. Simultaneously press buttons A and B (below the microphone: bit) to set the timer to 0.


step 2: starting and stopping the timer

we use the A button to start the timer, and the B button to stop it.

  • your timer should start when the button is pressed. Add a new onPressA to your script.


  • the timer will rely as long as the B button has not been pressed. to do this, first drag a block while to your new event onPressA .


  • click the arrow, and select ! value .


  • you can then drag a buttonBPressed in value empty in your while .


all code inside this while will be executed in a loop, as B button has been pressed.

  • Add a wait of 1000ms, this allows to count time in seconds.


  • to increase your time drag the in the variable, click the arrow and then click left + right .


  • now click on left and choose your time .


  • click right and enter 1 . This will add 1 to the current value of the time .


  • here is what your code should look like:


  • click on “ run ‘ to test your code.

  • press buttons A and B at the same time to reset your timer 0

  • press the A button to start your timer

  • press and hold the B button to stop your timer.


challenge your friends!

use the timer to challenge your friends. For example, you can see how long it takes them to say the alphabet backwards, or appoint 10 capitals.

help video

Article inspired by: https://href.li/?https://www.codeclubprojects.org/en-GB/microbit/against-the-clock/

No comments:

Powered by Blogger.