pythonesp8266micropythonneopixel

esp8266 - micropython - neopixel. how i can turn off previous led when next one go forward?


I'm trying to get the led to go forward like a second hand.

np = neopixel.NeoPixel(machine.Pin(2), led)
while True:
    t = utime.localtime()
    h = int(utime.localtime()[3]) + time_zone
    m = utime.localtime()[4]
    s = utime.localtime()[5]
    np[h] = (64, 64, 64)
    np[m] = (64, 64, 64)
    np[s] = (64, 64, 64)#turn on
    time.sleep(1)
    sc = s - 1
    np[sc] = (0, 0, 0)#turn off
    np.write()

but i think my code is not a good idea.


Solution

  • I'm not entirely clear how you want your display to look, but maybe this helps. With the code below, the "seconds" led will never overwrite the "hours" or "minutes" led.

    Critically, we also reset everything to "off" other than the leds we're lighting for the current hour, minute, and second.

    import machine
    import neopixel
    
    # on my micropython devices, 'time' and 'utime' refer to the same module
    import time
    
    np = neopixel.NeoPixel(machine.Pin(2), 60)
    
    while True:
        t = time.localtime()
        h, m, s = (int(x) for x in t[3:6])
    
        # set everything else to 0
        for i in range(60):
            np[i] = (0, 0, 0)
    
        np[s] = (0, 0, 255)
        np[m] = (0, 255, 0)
        np[h] = (255, 0, 0)
    
        np.write()
        time.sleep(0.5)
    

    I don't have a neopixel myself, so I wrote a simulator that I used for testing this out with regular Python.