#include <Adafruit_NeoPixel.h>
// 1. 定义灯带总数
const int NUM_STRIPS = 2;
// 2. 定义每个灯带的数据引脚
uint8_t stripPins[NUM_STRIPS] = {6, 7};
// 3. 定义每个灯带的像素数量
int stripPixelCounts[NUM_STRIPS] = {16, 16};
// 为每一条灯带预设一个颜色 (顺序与上面的灯带对应)
// 使用 strip.Color(R, G, B) 格式
uint32_t stripColors[NUM_STRIPS] = {
Adafruit_NeoPixel::Color(255, 0, 0), // 第1条灯带 (索引0): 红色
Adafruit_NeoPixel::Color(0, 255, 0), // 第2条灯带 (索引1): 绿色
};
// 4. 创建一个指针数组,用于存放所有灯带对象
Adafruit_NeoPixel* strips[NUM_STRIPS];
void setup() {
// 5. 使用循环初始化每一条灯带
for (int i = 0; i < NUM_STRIPS; i++) {
// 为数组中的每个元素创建一个新的 NeoPixel 对象
strips[i] = new Adafruit_NeoPixel(stripPixelCounts[i], stripPins[i], NEO_GRB + NEO_KHZ800);
strips[i]->begin(); // 初始化这条灯带
strips[i]->show(); // 熄灭所有像素
strips[i]->setBrightness(50); // 设置亮度
}
}
void loop() {
for (int i = 0; i < NUM_STRIPS; i++) {
// 使用 fill() 函数将整条灯带设置为指定颜色
strips[i]->fill(stripColors[i]);
// 调用 show() 来更新灯带的实际显示
strips[i]->show();
}
delay(1000);
// --- 示例2: 用循环熄灭所有灯带 ---
for (int i = 0; i < NUM_STRIPS; i++) {
strips[i]->clear();
strips[i]->show();
}
delay(1000);
}