import utime
# Initialize variables
start_ticks = None
is_timer_running = False
while True :
# Function to start the timer
def start_timer():
global start_ticks, is_timer_running
start_ticks = utime.ticks_ms()
print ("start_ticks",start_ticks)
is_timer_running = True
# Function to stop the timer
def stop_timer():
global start_ticks, is_timer_running
start_ticks = None
is_timer_running = False
# Function to get the elapsed time in milliseconds
def get_elapsed_time():
if start_ticks is not None:
current_ticks = utime.ticks_ms()
return utime.ticks_diff(current_ticks, start_ticks)
else:
return 0 # Counter is at zero if time measurement is stopped
# Example usage
start_timer() # Start the timer
# Simulate some work (e.g., a delay of 2 seconds)
utime.sleep(2) # Sleep for 2 seconds
# Get the elapsed time
elapsed_time = get_elapsed_time()
print("Elapsed time: {} milliseconds".format(elapsed_time))
# Stop the timer
stop_timer()
# Simulate more work (e.g., another delay of 1 second)
utime.sleep(1) # Sleep for 1 second
# Get the elapsed time after stopping (should be the same as before stopping)
elapsed_time_after_stop = get_elapsed_time()
print("Elapsed time after stopping: {} milliseconds".format(elapsed_time_after_stop))