Step 1: Make it
What is it?
Make a real stopwatch using the new micro:bit's touch logo sensor as an extra button.
What you'll learn
- 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
How to use it
- 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.
How it works
- The micro:bit tracks how long it's been powered on in milliseconds (thousandths 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.
What you need
- new micro:bit with sound (or MakeCode simulator)
- MakeCode or Python editor
- battery pack (optional)
Step 2: Code it
1from microbit import * 2time = 0 3start = 0 4running = False 5 6while True: 7 if running: 8 display.show(Image.HEART) 9 sleep(300) 10 display.show(Image.HEART_SMALL) 11 sleep(300) 12 else: 13 display.show(Image.ASLEEP) 14 if button_a.was_pressed(): 15 running = True 16 start = running_time() 17 if button_b.was_pressed(): 18 if running: 19 time += running_time() - start 20 running = False 21 if pin_logo.is_touched(): 22 if not running: 23 display.scroll(int(time/1000))
Step 3: Improve it
- 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.
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.