Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
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.
Ensure you have the timer.py
file on your CIRCUIT.PY
lib folder.
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)
time
from CircuitPython and a Timer
object from a custom or external module named timer
.timer = Timer()
initializes the Timer instance.timer.set_timer(start_time, end_time)
sets the timer for 10 seconds using CircuitPython’s time.monotonic()
.
timer.check_timer()
is called to see if the timer has reached the set time. If true, it prints “Timer reached!” otherwise it informs you that the timer is still running.time.sleep(0.5)
ensures the loop checks the timer every half-second, preventing excessive CPU usage.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.
“””