Detailed information about this class can be found in
The Chronometer can be used to measure how much time has elapsed in a block of code. The following example uses a simple stopwatch.
from machine import Timerimport timechrono = Timer.Chrono()chrono.start()time.sleep(1.25) # simulate the first lap took 1.25 secondslap = chrono.read() # read elapsed time without stoppingtime.sleep(1.5)chrono.stop()total = chrono.read()print()print("\nthe racer took %f seconds to finish the race" % total)print(" %f seconds in the first lap" % lap)print(" %f seconds in the last lap" % (total - lap))
The Alarm can be used to get interrupts at a specific interval. The following code executes a callback every second for 10 seconds.
from machine import Timerclass Clock:def __init__(self):self.seconds = 0self.__alarm = Timer.Alarm(self._seconds_handler, 1, periodic=True)def _seconds_handler(self, alarm):self.seconds += 1print("%02d seconds have passed" % self.seconds)if self.seconds == 10:alarm.callback(None) # stop counting after 10 secondsclock = Clock()