8. utime
utime module based upon the time module in the Python standard library.8.1. sleep
utime.sleep may be convenient for micro sleeps or sleeps in seconds.- utime.sleep(seconds)
Sleep for the given number of seconds.
secondsis an int or float.
from microbit import *
import utime
while True:
display.scroll(".")
utime.sleep(2)
- utime.sleep_ms(ms)
Delay for given number of milliseconds.
msis a positive int or 0.
from microbit import *
import utime
while True:
for x in range(5):
display.clear()
display.set_pixel(x, 0, 9)
utime.sleep_ms(200)
- utime.sleep_us(us)
Delay for given number of microseconds.
usis a positive int or 0.
from microbit import *
import utime
for i in range(1000):
pin0.write_digital(1)
utime.sleep_us(500)
pin0.write_digital(0)
utime.sleep_us(500)
8.2. ticks
- utime.ticks_ms()
Returns an increasing millisecond counter with an arbitrary reference point, that wraps around after some value. This counter is useful for measuring time intervals and implementing delays.
from microbit import *
import utime
import random
while True:
display.clear()
sleep(random.randint(1000, 4000))
display.show(Image.HEART)
start = utime.ticks_ms()
while not button_a.is_pressed():
utime.sleep_ms(1)
reaction = utime.ticks_diff(utime.ticks_ms(), start)
display.scroll(str(reaction) + "ms", delay=80)
- utime.ticks_us()
Returns an increasing microsecond counter with an arbitrary reference point, that wraps around after some value.
from microbit import *
import utime
start = utime.ticks_us()
total = 0
for i in range(1000):
total += i
duration = utime.ticks_diff(utime.ticks_us(), start)
display.scroll(str(duration) + "us", delay=80)
8.3. ticks_add
- utime.ticks_add(ticks, delta)
Offset ticks value by a given number, which can be either positive or negative. Given a ticks value, this function allows to calculate a ticks value, delta ticks before or after it.
from microbit import *
import utime
import usys
# Find out TICKS_MAX used e.g. 1073741823
val = utime.ticks_add(0, -1)
# print(val)
display.scroll(str(val), delay=80)
max_sys = usys.maxsize
# print(max_sys/val)
display.scroll(str(max_sys/val), delay=80)
8.4. ticks_diff
- utime.ticks_diff(ticks1, ticks2)
Measure ticks difference between values returned from
utime.ticks_ms()orutime.ticks_us()functions, as a signed value which may wrap around.The argument order is the same as for subtraction operator,
ticks_diff(ticks1, ticks2)has the same meaning asticks1 - ticks2.
from microbit import *
import utime
start = None
while True:
if button_a.was_pressed():
start = utime.ticks_ms()
display.show(Image.ARROW_E)
if button_b.was_pressed() and start is not None:
elapsed = utime.ticks_diff(utime.ticks_ms(), start)
display.scroll(round(elapsed / 1000, 1), delay=80)
start = None
sleep(10)
8.5. TIMED_OUT
from microbit import *
import utime
display.scroll(pin2.read_digital())
start = utime.ticks_ms()
while pin2.read_digital() == 0:
if utime.ticks_diff(utime.ticks_ms(), start) > 2000:
display.scroll("TIMED_OUT")
break
8.6. Deadline
from microbit import *
import utime
timer = 3000
deadline = utime.ticks_add(utime.ticks_ms(), timer)
while utime.ticks_diff(deadline, utime.ticks_ms()) > 0:
utime.sleep_ms(200)
display.show(Image.SKULL)
8.7. countdown timer
from microbit import *
import utime
# Set the countdown timer to 10 seconds (10000 milliseconds)
countdown_time = 10000
deadline = utime.ticks_add(utime.ticks_ms(), countdown_time)
while utime.ticks_diff(deadline, utime.ticks_ms()) > 0:
# Calculate the remaining time
remaining_time = utime.ticks_diff(deadline, utime.ticks_ms())
# Convert remaining time to seconds by integer division and display it
display.show(str(remaining_time // 1000))
# Sleep for a short period to update the display
utime.sleep_ms(200)
# Display a smiley face when the countdown is complete
display.show(Image.HAPPY)