Page 1 of 1
Timer
Posted: Fri Mar 01, 2019 8:06 am
by Nikunj_Vadher
I am exploring micropython.
I came across a task I need to perform 2 separate operation at different interval.
For example:
1st task at interval of 5 second
2nd task at interval of 45 minutes.
My question is how to create 2 different timers running on different operation and how to put resolution of minutes in timer ?
Thank You.
Re: Timer
Posted: Fri Mar 01, 2019 1:06 pm
by larsks
You can create multiple virtual timers as described in the docs at
https://docs.micropython.org/en/latest/ ... tml#timers. For a 5-second timer that would look something like:
Code: Select all
from machine import Timer
def t1_callback_function(t):
print('this is timer1')
t1 = Timer(-1)
t1.init(period=5000, mode=Timer.PERIODIC, callback=t1_callback_function)
For a 45 minute timer, you're going to need to count minutes by yourself. Something like:
Code: Select all
from machine import Timer
def t2_callback_function(t):
global t2_counter
t2_counter -= 1
if t2_counter == 0:
t2_counter = 45
print('this is timer2')
t2_counter = 45
t2 = Timer(-1)
t2.init(period=60000, mode=Timer.PERIODIC, callback=t2_callback_function)
In the above, we are calling the callback function once/minute. It decrements the global t2_counter variable until it reaches 0, then it resets the counter and performs the target action.
Re: Timer - periodic limit
Posted: Wed Mar 06, 2019 10:07 pm
by emtee
Hey larsks,
You seem to be implying that there is a limit for the length of the Timer period. I am trying to use a Timer with a period of 60 minutes and I am getting inconsistent results. Double-checked the documentation and there doesn't appear to be any mention that a Timer has a limit of (for example) 600,000 milliseconds (i.e. 10 minutes).
Is there a limit for the Timer? If so, how many milliseconds?