Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

Creating a Simple Timer Using CircuitPython

This tutorial will guide you through creating a simple timer in CircuitPython using the Timer module. We will create a basic timer functionality that runs for a specific period (like 10 seconds) before notifying the user that the time has elapsed.

Libraries

Ensure you have the timer.py file on your CIRCUIT.PY lib folder.


Code

import time
from timer import Timer

# Create an instance of the Timer
timer = Timer()

# Setting a timer for 10 seconds
timer.set_timer(time.monotonic(), time.monotonic() + 10)

# Continuously checks if the timer has ended
while True:
    # Check the timer status
    timer_end = timer.check_timer()
    
    # Provide feedback based on the timer status
    if timer_end:
        print("Timer reached!")
        break
    else:
        print("Timer still running")
    
    # Wait half a second before checking again
    time.sleep(0.5)

Code Breakdown

Experiment

Try changing the timer.set_timer()’s second argument to see how the duration affects the output timing. For example, change time.monotonic() + 10 to time.monotonic() + 5 for a shorter duration. “””