【问题标题】:esp8266 - micropython - neopixel. how i can turn off previous led when next one go forward?esp8266 - micropython - neopixel。下一个前进时如何关闭前一个 LED?
【发布时间】:2021-05-01 14:38:29
【问题描述】:

我正试图让 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()

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

【问题讨论】:

  • 你有什么样的新像素?运行代码时会发生什么?另外,led 的值是多少?
  • WS2812。 60条灯带。当我添加分针时,秒针经过时会关闭分针。

标签: python esp8266 micropython neopixel


【解决方案1】:

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

至关重要的是,除了我们为当前小时、分钟和秒点亮的 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)

我自己没有neopixel,所以我写了一个simulator,我用它来用普通的Python测试它。

【讨论】:

  • 谢谢拉尔斯克。我会在家里试试你的解决方案。我会看看你的刺激器
  • 再次感谢!效果很好,你的模拟器也一样!
猜你喜欢
  • 2021-06-19
  • 2015-02-28
  • 2020-10-24
  • 2017-01-11
  • 2014-11-16
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
相关资源
最近更新 更多