Skip to content

Commit 07d467e

Browse files
committed
Add micro:bit timer examples and update todo
Added two Python scripts demonstrating the use of running_time() on micro:bit: one for printing elapsed time and another for a countdown timer with button interaction. Updated todo.md to include timers on micro:bit.
1 parent e857138 commit 07d467e

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# running_time()
2+
from microbit import *
3+
4+
'''
5+
The microbit has a very simple timing mechanism called running_time().
6+
It keeps a running count of the number of the time since
7+
the micro:bit started in milliseconds.
8+
'''
9+
10+
while True:
11+
print(running_time())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# countdown timer
2+
from microbit import *
3+
import music
4+
5+
'''
6+
To create a countdown timer, we will use a built-in function called running_time().
7+
running_time() keeps a running count of the number of the time since
8+
the micro:bit started in milliseconds.
9+
'''
10+
11+
# --- SETUP
12+
# start components
13+
14+
# store variables
15+
running = False
16+
timer = 5000
17+
18+
# --- RUNNING
19+
while True:
20+
# read sensor data
21+
a_pressed = button_a.was_pressed()
22+
23+
# process data
24+
if a_pressed:
25+
running = True # start the countdown timer
26+
start = running_time() # records the starting time
27+
if running:
28+
lapsed_time = running_time() - start # calculates how long since button press
29+
if timer - lapsed_time < 0: # checks if countdown has reached 0
30+
music.pitch(500,250, wait=False)
31+
running = False # stops the timer
32+
33+
# output data
34+

‎todo.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# To Do
22

3-
- [ ] Servo Drivers
3+
- [ ] Timers on micro:bit

0 commit comments

Comments
 (0)