esp8266 micropython Neopix。如何在下一个led灯亮起时关闭上一个led灯?

2024-10-01 02:21:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让led像秒针一样向前移动

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()

但我认为我的代码不是一个好主意


Tags: trueledtimenppinmachineturnint
1条回答
网友
1楼 · 发布于 2024-10-01 02:21:41

我不完全清楚您希望显示器的外观,但这可能会有所帮助。使用下面的代码,“秒”led将永远不会覆盖“小时”或“分钟”led

关键的是,我们还将除当前小时、分钟和秒点亮的LED之外的所有设备重置为“关闭”

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)

我自己没有Neopix,所以我写了一个simulator,我用它来用常规Python测试

相关问题 更多 >