Page 1 of 1

can the Timer be used twice at the same time?

Posted: Fri Feb 07, 2020 1:41 pm
by timer
I wrote the code like that,

Code: Select all

from machine import Timer

class TOUCH(object):
    def __init__(self,pin):
        self.pin = pin
        self.tim = Timer(4)
        self.tim.init(mode=Timer.PERIODIC, period=2000,callback=self.touch_cb)
    def touch_cb(self, tim):
        print(self.pin)
    def stop(self):
        self.tim.deinit()

a = TOUCH("1")
print(a)
dir(a)
b = TOUCH("2")
print(b)
dir(b)

while 1:
    pass
I need to check that more than one button's status immediately.
and send the status via websocket.

Re: can the Timer be used twice at the same time?

Posted: Wed Feb 12, 2020 3:26 am
by jimmo
You cannot use the same timer twice like that.

Writing machine.Timer(4) will always return back the same timer instance.

Either you should use a different timer instance for each timer, or find a way to make them use the same timer (i.e. if they're being checked at the same time, why do you need two timers?).