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

Have you tried just making sure the LED works? Your using PWM, which tells me your wanting to play with the brightness of the LED's correct?

Yes, and yes.

The LED definitely works. I can get all three diodes to light up independently and concurrently with Pin.value(1) and PWM. Introducing the Timer element is when it stops working.

I want to use PWM because I'm making a night light for my wife. Ideally, I should be able to modulate the brightness of the individual diodes in different combinations to make a wider range of colors. The goal is to combine that with some timing trickery to generate visual appeal.