micropython ws2812b 渐变灯

import machine
import neopixel
import time
# 设置灯带参数
NUM_LEDS = 60  # 灯珠数量
PIN = 5  # 灯带数据引脚
BRIGHTNESS = 1  # 亮度系数(0.0到1.0)

# 初始化灯带对象
np = neopixel.NeoPixel(machine.Pin(PIN), NUM_LEDS)

# 渐变函数
def gradient(color1, color2, steps):
    r1, g1, b1 = color1
    r2, g2, b2 = color2
    step_r = (r2 - r1) / steps
    step_g = (g2 - g1) / steps
    step_b = (b2 - b1) / steps
    for i in range(steps):
        r = int(r1 + (i * step_r))
        g = int(g1 + (i * step_g))
        b = int(b1 + (i * step_b))
        yield (r, g, b)

# 渐变循环
while True:
    # 渐变从红色到绿色
    for color in gradient((255, 0, 0), (0, 255, 0), NUM_LEDS):
        np.fill((int(color[0]*BRIGHTNESS), int(color[1]*BRIGHTNESS), int(color[2]*BRIGHTNESS)))
        np.write()
        time.sleep(0.1)
    # 渐变从绿色到蓝色
    for color in gradient((0, 255, 0), (0, 0, 255), NUM_LEDS):
        np.fill((int(color[0]*BRIGHTNESS), int(color[1]*BRIGHTNESS), int(color[2]*BRIGHTNESS)))
        np.write()
        time.sleep(0.1)
    # 渐变从蓝色到红色
    for color in gradient((0, 0, 255), (255, 0, 0), NUM_LEDS):
        np.fill((int(color[0]*BRIGHTNESS), int(color[1]*BRIGHTNESS), int(color[2]*BRIGHTNESS)))
        np.write()
        time.sleep(0.1)

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值