arduino编程LED灯带示例
灯带使用的是WS2812,FastLED库是第三方库,网上有下载和导入教程,实现了3种效果,流水,静态彩虹,闪烁。
#include "FastLED.h" // FastLED库
#define NUM_LEDS 60 // LED灯珠数量
#define DATA_PIN 2 // Arduino输出控制信号引脚
#define LED_TYPE WS2812 // LED灯带型号
#define COLOR_ORDER GRB // RGB灯珠中红色、绿色、蓝色LED的排列顺序
uint8_t max_bright = 128; // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
CRGB leds[NUM_LEDS]; // 建立光带leds
void led_rainbow_loop(void){
FastLED.clear();
FastLED.show();
delay(500);
fill_solid(leds, 60, CRGB::Red);
FastLED.show();
delay(1000); // 等待1秒
fill_solid(leds, 60, CRGB::Orange);
FastLED.show();
delay(1000); // 等待1秒
fill_solid(leds, 60, CRGB::Yellow);
FastLED.show();
delay(1000); // 等待1秒
fill_solid(leds, 60, CRGB::Green);
FastLED.show();
delay(1000); // 等待1秒
fill_solid(leds, 60, CRGB::Cyan);
FastLED.show();
delay(1000); // 等待1秒
fill_solid(leds, 60, CRGB::Blue);
FastLED.show();
delay(1000); // 等待1秒
fill_solid(leds, 60, CRGB::Purple);
FastLED.show();
delay(1000); // 等待1秒
}
void led_rainbow_flowing(void)
{
FastLED.clear();
FastLED.show();
delay(500);
int i = 0,loop_count = 0;
if(NUM_LEDS%7 == 0)
{
loop_count = NUM_LEDS/7;
}
else
{
loop_count = NUM_LEDS/7+1;
}
for(i=0; i<loop_count; i++)
{
leds[i*7] = CRGB::Red;
FastLED.show();
delay(200);
if(i*7+1>=NUM_LEDS)
{
break;
}
leds[i*7+1] = CRGB::Orange;
FastLED.show();
delay(200);
if(i*7+2>=NUM_LEDS)
{
break;
}
leds[i*7+2] = CRGB::Yellow;
FastLED.show();
delay(200);
if(i*7+3>=NUM_LEDS)
{
break;
}
leds[i*7+3] = CRGB::Green;
FastLED.show();
delay(200);
if(i*7+4>=NUM_LEDS)
{
break;
}
leds[i*7+4] = CRGB::Cyan;
FastLED.show();
delay(200);
if(i*7+5>=NUM_LEDS)
{
break;
}
leds[i*7+5] = CRGB::Blue;
FastLED.show();
delay(200);
if(i*7+6>=NUM_LEDS)
{
break;
}
leds[i*7+6] = CRGB::Purple;
FastLED.show();
delay(200);
if(i*7+7>=NUM_LEDS)
{
break;
}
}
}
void led_rainbow_static(void){
FastLED.clear();
FastLED.show();
delay(500);
fill_rainbow(leds, 60, 0, 15);
FastLED.show();
delay(1000);
}
void setup() {
LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // 初始化光带
FastLED.setBrightness(max_bright); // 设置光带亮度
}
void loop() {
while(1){
led_rainbow_flowing();
led_rainbow_static();
led_rainbow_loop();
}
}