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)
micropython ws2812b 渐变灯
最新推荐文章于 2024-08-16 08:45:53 发布