Micro:Bit Stop Watch
Lesson Objectives:
- Students will learn about while loops
- Students will learn to use micro-controllers
Materials:
Goal:
Create a stopwatch with a micro:bit that records how long a button is pressed and displays the result.
You will want to make use of the micro:bit function called microbit.running_time() which returns the number of milliseconds since the micro:bit started running.
Create a stopwatch with a micro:bit that records how long a button is pressed and displays the result.
You will want to make use of the micro:bit function called microbit.running_time() which returns the number of milliseconds since the micro:bit started running.
Features:
Challenge: Use an animated and accurate analog clock face as the indicator
Micro:bit includes images of clocks in the microbit library. In fact, there is even a list of clock faces Image.ALL_CLOCKS[clock_num] where you replace clock number with the "hour" that you want the clock to point to, integers 1 through 12. Use this to animate a clocks second hand that points to 1 o'clock after 5 seconds (5000 ms) have elapsed (and up to 10 seconds), 2 o'clock after 10 seconds (and up to 15), etc. After a full minute, your animation should loop back around like a real stopwatch. For example, 1 minute and 15 seconds should be pointing at 3 o'clock.
- While button A is pressed, the stopwatch should continually update the elapsed time.
- LED "timing" Indicator on the micro:bit's screen while the button is pressed to tell you that the stopwatch is "running".
- When button A is released the stopwatch should display the elapsed time on the LED display.
- After the time elapsed is displayed, the stopwatch should reset and loop back to be ready for timing something else.
Challenge: Use an animated and accurate analog clock face as the indicator
Micro:bit includes images of clocks in the microbit library. In fact, there is even a list of clock faces Image.ALL_CLOCKS[clock_num] where you replace clock number with the "hour" that you want the clock to point to, integers 1 through 12. Use this to animate a clocks second hand that points to 1 o'clock after 5 seconds (5000 ms) have elapsed (and up to 10 seconds), 2 o'clock after 10 seconds (and up to 15), etc. After a full minute, your animation should loop back around like a real stopwatch. For example, 1 minute and 15 seconds should be pointing at 3 o'clock.
Code
Step 1 Code
References
The following functions (documentation copied from microBit microPython) documentation may be helpful. The Image and Display classes are also relevant.
The full documentation may be found here:
is_pressed()
Returns True if the specified button button is currently being held down, and False otherwise.
microbit.display.show(image)
Display the image.
microbit.display.scroll(value, delay=150, *, wait=True, loop=False, monospace=False)
Scrolls value horizontally on the display. If value is an integer or float it is first converted to a string using str(). The delay parameter controls how fast the text is scrolling.
If wait is True, this function will block until the animation is finished, otherwise the animation will happen in the background.
If loop is True, the animation will repeat forever.
If monospace is True, the characters will all take up 5 pixel-columns in width, otherwise there will be exactly 1 blank pixel-column between each character as they scroll.
Note that the wait, loop and monospace arguments must be specified using their keyword.
The following functions (documentation copied from microBit microPython) documentation may be helpful. The Image and Display classes are also relevant.
The full documentation may be found here:
is_pressed()
Returns True if the specified button button is currently being held down, and False otherwise.
microbit.display.show(image)
Display the image.
microbit.display.scroll(value, delay=150, *, wait=True, loop=False, monospace=False)
Scrolls value horizontally on the display. If value is an integer or float it is first converted to a string using str(). The delay parameter controls how fast the text is scrolling.
If wait is True, this function will block until the animation is finished, otherwise the animation will happen in the background.
If loop is True, the animation will repeat forever.
If monospace is True, the characters will all take up 5 pixel-columns in width, otherwise there will be exactly 1 blank pixel-column between each character as they scroll.
Note that the wait, loop and monospace arguments must be specified using their keyword.