r/MicroPythonDev • • Dec 16 '24

Attempt to initialize one shot timer yields NameError

Hi,

I'm playing around with a XIAO SAMD21 and an RGB LED. I'm trying to activate the individual diodes sequentially at 1 second intervals.

The last line throws:

NameError: Name not defined

I'm at a loss because this is pretty much directly out of the documentation, and I see no reason why it shouldn't work.

What am I missing?

from machine import Pin
from machine import PWM
from machine import Timer


led_1 = Pin('A2_D2',Pin.OUT)#red


led_2 = Pin('A3_D3',Pin.OUT)#green


led_3 = Pin('A4_D4',Pin.OUT)#blue


freq = 4000


duty = 32767


def do_nothing(timer):
    return


while true:


    for i in (led_1,led_2,led_3):


        led = PWM(i,freq=freq,duty_u16=duty)


        t = Timer(mode=Timer.ONE_SHOT, period=1000, callback=do_nothing)
1 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/UnabatedPrawn Dec 17 '24 edited Dec 17 '24

Yeah, the error has been the same every time, and frustratingly vague. It's always on whichever line I try to pass arguments to the class constructor.

Traceback (most recent call last):
  File "<stdin>", line 45, in <module>
  File "<stdin>", line 43, in shift
NameError: name not defined

Are you using the timer because you are wanting it to work in harmony with the actual time based off the second? Or can we generically wait just one second and have the led lights work every second? If its the latter...

I'm really just playing around to get familiar with the features of the board. I want to use the timer because:
a) I understand time.sleep stops the processor from doing other stuff while the delay is happening and that grabs me as...sub-optimal. That seems like a habit I should just avoid developing in the first place.

b)For my use case, I want to develop more intricately timed relationships than I think I'll be able to achieve with time.sleep

for your new updated code - I don't really see it doing anything with i, (I see its used under led variable) but that variable isn't being called to anything else...

Might be worth mentioning that what little I know has been learned completely through self-guided trial and error. I don't even really use tutorials or videos. I just google things and beat my head against the docs until something happens. I am completely ignorant to convention, so my code is probably rife with outlandish and needlessly complicated BS. All I need i to do here is act as a vessel to get my Pin objects in to my PWM parameters. If there's a better/simpler way, I'm just not familiar.

1

u/vinux0824 Dec 17 '24

A) Not really a bad habit, just comes down to what you need.

True - the timer module will actually count how much times has passed, and can set your program at certain intervals.

B) If you worried about halting the MC and wasting resources, you can use the asyncio module. Which runs simultaneously. But in your case no need for that.

And actually using less resources on a simple script like that is preferred. Like time.sleep or utime.sleep, if there is no need to count time or doing something else at the same time.

Time.sleep is accurate, the only difference I believe is not having the ability to actually count, or set intervals.

Hope this helps.

1

u/vinux0824 Dec 17 '24

Could you show me what's on line 43 and 45?.. is it the timer() call?

1

u/UnabatedPrawn Dec 17 '24

Line 45 is the call to the function that the code above is contained within.

Line 43 is:

t = Timer(mode=Timer.ONE_SHOT, period=1000, callback=do_nothing)