can the Timer be used twice at the same time?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
timer
Posts: 3
Joined: Fri Feb 07, 2020 1:31 pm

can the Timer be used twice at the same time?

Post by timer » Fri Feb 07, 2020 1:41 pm

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.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Wed Feb 12, 2020 3:26 am

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?).

Post Reply