本帖最后由 jinglixixi 于 2021-9-25 13:15 编辑
ESP32-Audio-Kit音频开发板配合库文件FastLED.h的使用能轻松地控制WS2812彩色灯带的显示效果,所用的驱动引脚及灯带的点数均可通过参数进行调整。
以7点彩灯带为例,其显示程序如下:
#include <FastLED.h>
#define NUM_LEDS 7
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
}
void loop() {
leds[0] = CRGB::Red;
leds[1] = CRGB::Green;
leds[2] = CRGB::Blue;
leds[3] = CRGB::Red+CRGB::Green;
leds[4] = CRGB::Red+CRGB::Blue;
leds[5] = CRGB::Green+CRGB::Blue;
leds[6] = CRGB::Green+CRGB::Blue+CRGB::Red;
FastLED.show();
delay(500);
leds[0] = CRGB::Black;
leds[1] = CRGB::Black;
leds[2] = CRGB::Black;
leds[3] = CRGB::Black;
leds[4] = CRGB::Black;
leds[5] = CRGB::Black;
leds[6] = CRGB::Black;
FastLED.show();
delay(500);
}
经编译和上传,其结果如图1所示,图2是其运行效果。
图1完成上传
图2显示效果
对于60点的彩色灯带,其显示程序如下:
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
}
void loop() {
int i;
for(i=0;i<10;i++)
{
leds[0+i*6] = CRGB::Red;
leds[1+i*6] = CRGB::Green;
leds[2+i*6] = CRGB::Blue;
leds[3+i*6] = CRGB::Red+CRGB::Green;
leds[4+i*6] = CRGB::Red+CRGB::Blue;
leds[5+i*6] = CRGB::Green+CRGB::Blue;
}
FastLED.show();
delay(500);
for(i=0;i<10;i++)
{
leds[0+i*6] = CRGB::Black;
leds[1+i*6] = CRGB::Black;
leds[2+i*6] = CRGB::Black;
leds[3+i*6] = CRGB::Black;
leds[4+i*6] = CRGB::Black;
leds[5+i*6] = CRGB::Black;
}
FastLED.show();
delay(500);
}
其运行效果如图3所示:
图3显示效果