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.


specifications


  • 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.


1


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

2


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

3


  • 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 .

4


  • click update and # number and enter 0 .

5


here is what your code should look like:


6


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

7


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

8


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.

9


  • 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 .

10


  • click the arrow, and select ! value .

11


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

12


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.

13


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

14


  • now click on left and choose your time .

15


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

16


  • here is what your code should look like:

17


  • 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.

18


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.