Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4968

MicroPython • Re: callback from a timer ?

$
0
0
Yeah, the documentation is just unclear as it only says the constructor takes the same arguments as init() but doesn't mention that providing them starts the timer immediately. Behind the scenes, if those arguments are given, MP calls init() as part of timer creation.
Yes, you can either create a timer and then set it running later, or create a timer and set it running simultaneously.

The bigger problem with -

Code:

Timer(period=100, mode=Timer.PERIODIC, callback=acc_decc) 
Is that it creates a timer and sets it running, but doesn't assign a reference to the timer object such as this would -

Code:

myTimer = Timer(period=100, mode=Timer.PERIODIC, callback=acc_decc) 
Thus the reference is thrown away rather than assigned, is lef thanging around waiting to be garbage collected, and I am told things may go badly wrong once garbage has been collected.

And this is also the case if 'myTimer = Timer(....)' appears in a function because 'myTimer' disappears into the garbage when that function exits. One needs to make 'myTimer' a global to avoid that.

Code:

Timer(period=100, mode=Timer.PERIODIC, callback=acc_decc) 
Beyond all that, the OP's problem I think is in using 'mode=Timer.PERIODIC' rather than 'mode=Timer.ONE_SHOT'', that they have a flawed algorithm which won't work to start with.

And we are trying to second-guess what it is the OP wants to achieve.

Statistics: Posted by hippy — Tue Jan 16, 2024 1:57 pm



Viewing all articles
Browse latest Browse all 4968

Trending Articles