目录 / moving-rainbow
moving-rainbow
为使用树莓派Pico、NeoPixel灯条和按钮控制实现的“移动彩虹LED灯条”教育项目生成MicroPython程序。 以下是一个完整的MicroPython程序示例,适用于Raspberry Pi Pico控制NeoPixel灯条,实现彩虹颜色流动效果,并通过按钮控制启动/停止或切换模式: ```python import machine import neopixel import time import math # 配置引脚和NeoPixel参数 LED_PIN = 2 # 连接NeoPixel数据线的GPIO引脚 NUM_LEDS = 30 # LED灯珠数量 BRIGHTNESS = 0.3 # 亮度(0.0 ~ 1.0) # 初始化NeoPixel对象 np = neopixel.NeoPixel(machine.Pin(LED_PIN), NUM_LEDS) # 按钮配置(使用内部上拉电阻) BUTTON_PIN = 15 button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP) # 全局变量 running = False mode = 0 # 0: 关闭, 1: 彩虹流动, 2: 彩虹呼吸, 3: 彩虹追逐 last_button_time = 0 debounce_delay = 200 # 防抖延迟(毫秒) def wheel(pos): """生成彩虹颜色(0-255)""" if pos < 85: return (pos * 3, 255 - pos * 3, 0) elif pos < 170: pos -= 85 return (255 - pos * 3, 0, pos * 3) else: pos -= 170 return (0, pos * 3, 255 - pos * 3) def rainbow_cycle(wait): """彩虹循环效果""" for j in range(256): for i in range(NUM_LEDS): pixel_index = (i * 256 // NUM_LEDS) + j np[i] = wheel(pixel_index & 255) np.write() time.sleep(wait) def rainbow_breathing(wait): """彩虹呼吸效果""" for j in range(256): brightness = (math.sin(j * math.pi / 128) + 1) / 2 * BRIGHTNESS for i in range(NUM_LEDS): pixel_index = (i * 256 // NUM_LEDS) + j r, g, b = wheel(pixel_index & 255) np[i] = (int(r * brightness), int(g * brightness), int(b * brightness)) np.write() time.sleep(wait) def rainbow_chase(wait): """彩虹追逐效果""" for j in range(256): for i in range(NUM_LEDS): if (i + j) % 4 == 0: np[i] = wheel((i * 256 // NUM_LEDS + j) & 255) else: np[i] = (0, 0, 0) np.write() time.sleep(wait) def clear_strip(): """关闭所有LED""" for i in range(NUM_LEDS): np[i] = (0, 0, 0) np.write() def button_pressed(pin): """按钮中断处理函数""" global running,...
这是模型对公开材料的总结,不是官方声明,请以原链内容为准。
存档时间线
| 版本 | 存档时间 | 内容哈希 | 内容 |
|---|---|---|---|
| v1 | 2026-11-13 09:55 | 7dd532ae | 可取 |
版本索引永久保留;内容副本只保留最新 1 份,更早版本仅留索引与哈希(存档时间线的证据链不会因此断裂)。
提交举报 / 纠错
侵权举报经核验成立后,我们会即时下线该条目并删除已存的内容副本。