Skip to content

Activité

Touch stopwatch

Avancé | MakeCode, Python | Boutons, Touch logo, Écran LED | Entrée / Sortie, Logique booléenne, Opérateurs arithmétiques, Variables

Étape 1 : Maîtrisez

Qu'est-ce que c'est ?

Make a real stopwatch using the new micro:bit's touch logo sensor as an extra button.

Introduction

Guide de codage

Ce que vous apprendrez

  • How to use the new micro:bit's touch logo sensor as an extra button in a practical project
  • How to use variables and mathematical operators to measure time
  • How to use mathematical operators to convert units (milliseconds to seconds)
  • What a Boolean variable is and how they can be used to control the flow of a program

Comment l’utiliser 

  • Flash the program onto a new micro:bit with built-in speaker.
  • Press button A to start the stopwatch running. An animated beating heart is shown on the LED display while it's timing.
  • Press button B to stop it. You can start and stop it as many times as you like and it will keep adding to the time, just like a real stopwatch.
  • Press the gold touch logo on the front of the micro:bit to show the measured time in seconds.
  • To reset the time back to zero, press the reset button on the back of the micro:bit.

Comment ça marche

  • The micro:bit tracks how long it's been powered on in milliseconds (thousands of a second). This is called the running time.
  • When you press button A, a variable called start is set to the current running time.
  • When you press button B, the start time is subtracted from the new running time to work out how much time has elapsed since you started the stopwatch. This difference is added to the total time, which is stored in a variable called time.
  • If you press the touch logo, the program shows the total elapsed time on the LED display. it converts the time from milliseconds (thousandths of a second) to seconds by dividing it by 1000. It uses the integer division operator to give a result in whole numbers (integers).
  • The program also uses a Boolean variable called running to control the program. Boolean variables can only have two values: true or false. If running is true, the stopwatch has been started. If running is false, the stopwatch hasn't been started or has been stopped.
  • If running is true, a loop keeps the heart animation appearing on the LED display.
  • It will only show the time when you press the logo if the stopwatch has been stopped, if running is not true.
  • The code prevents false readings by making sure the time variable is only changed when you press button B if the stopwatch has already been started, if running is true.

Ce dont vous aurez besoin

  • new micro:bit with sound (or MakeCode simulator)
  • MakeCode ou éditeur Python
  • un boîtier de piles (optionnel)

Étape 2 : Codez

Étape 3 : Améliorez

  • Modify the program so you can reset the time by shaking the micro:bit.
  • Make the timer more accurate by using fractions of numbers instead of integers (whole numbers).
  • Add a lap time function so that if if you touch the logo while the stopwatch is running it shows the time at that point. Remember to make sure this is not added to the total time variable.