Does CircuitPython have a non-blocking Timerfunction?
Re: Does CircuitPython have a non-blocking Timerfunction?
now = time.monotonic()
if now >= time_to_check: #only check each second
# do stuff that should happen once per second
time_to_check = now + 1.0
Re: Does CircuitPython have a non-blocking Timerfunction?
Re: Does CircuitPython have a non-blocking Timerfunction?
now = time.monotonic()
now_ns = time.monotonic_ns()
print ('Now:',now * 100.0 , ' nanosec:', now_ns)
Re: Does CircuitPython have a non-blocking Timerfunction?
import board
import digitalio
import time
def TimePeriodIsOver(TimerVar, Period):
now = time.monotonic()
if TimerVar - now >= (Period /1000):
TimerVar = now + Period
return True
else:
return False
TimerVar = time.monotonic()
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
duration = 0.1
print('Hello 4 World!')
now = time.monotonic()
now_ns = time.monotonic_ns()
print ('Now:',now, ' nanosec:', now_ns)
while True:
if TimePeriodIsOver(TimerVar, 500) == True:
if led.value == True:
led.value = False
now = time.monotonic()
now_ns = time.monotonic_ns()
print ('Now:',now, ' nanosec:', now_ns)
else:
led.value = True
Re: Does CircuitPython have a non-blocking Timerfunction?
Re: Does CircuitPython have a non-blocking Timerfunction?
def TimePeriodIsOver(TimerVar, Period):
now = time.monotonic()
if TimerVar - now >= (Period /1000):
return True, now + Period
else:
return False, TimerVar
result, TimerVar = TimePeriodIsOver(TimerVar, 500)
if result:
...
def TimePeriodIsOver(Period):
global TimerVar
now = time.monotonic()
if TimerVar - now >= (Period /1000):
TimerVar = now + Period
return True
else:
return False
Re: Does CircuitPython have a non-blocking Timerfunction?
Re: Does CircuitPython have a non-blocking Timerfunction?
Re: Does CircuitPython have a non-blocking Timerfunction?
Re: Does CircuitPython have a non-blocking Timerfunction?
dastels wrote:Yes, this is generally done using time.monotonic(). In the main loop you have something llike:
- Code: Select all | TOGGLE FULL SIZE
now = time.monotonic()
if now >= time_to_check: #only check each second
# do stuff that should happen once per second
time_to_check = now + 1.0
Start by having a variable to track the time that something should happen, then check in the loop if it's that time yet, if so do the thing and set the next time. You can have multiple things happening this way at different times/intervals with a time variable and if clause for each.
Dave