#include
#define LED_PIN 5 //连接WS2812B灯带 D5
#define NUM_LEDS 60 //LED数量(可更改)
#define BRIGHTNESS 64 //亮度(可更改最大为100)
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// 此示例显示了设置和使用颜色“调色板”的几种方法
// 使用FastLED.
/*
// 这些紧凑的调色板为您重新着色提供了简便的方法
// 动态,快速,轻松且开销低的动画。
//
// 实际上,使用调色板比理论上更简单,所以首先
// 运行此项目,并观看漂亮的灯光编码。
// 尽管此草图具有八种(或更多)不同的配色方案,
// 整个项目在AVR上可编译为大约6.5K程序。
//
// FastLED提供了一些预配置的调色板,并使其成为
// 非常容易用调色板组成自己的配色方案。
//
// 有关以下内容的更抽象的“理论和实践”的一些注释
// FastLED紧凑调色板的解释位于此文件的底部。
*/
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
void setup() {
delay( 3000 ); // 上电安全延迟
FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void loop()
{
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* 运动速度 */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}
/*
// 这里展示了几种不同的调色板。
//
// FastLED提供了几种“预设”调色板:RainbowColors_p,RainbowStripeColors_p,
// OceanColors_p,CloudColors_p,LavaColors_p,ForestColors_p和PartyColors_p。
//
// 另外,您可以手动定义自己的调色板,也可以编写
// 即时创建调色板的代码。 全部显示在这里。
*/
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99;
if( lastSecond != secondHand) {
lastSecond = secondHand;
if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
}
}
// 此功能使用完全随机的颜色填充调色板。
void SetupTotallyRandomPalette()
{
for( int i = 0; i < 16; i++) {
currentPalette[i] = CHSV( random8(), 255, random8());
}
}
/*
// 此函数设置了黑白条纹的调色板,
// 使用代码。 由于调色板实际上是一个数组
// 16种CRGB颜色,可以使用各种fill_ *函数
// 进行设置。
*/
void SetupBlackAndWhiteStripedPalette()
{
// “遮盖”所有16个调色板条目...
fill_solid( currentPalette, 16, CRGB::Black);
// 并将每四分之一设置为白色。
currentPalette[0] = CRGB::White;
currentPalette[4] = CRGB::White;
currentPalette[8] = CRGB::White;
currentPalette[12] = CRGB::White;
}
// 此功能设置了紫色和绿色条纹的调色板。
void SetupPurpleAndGreenPalette()
{
CRGB purple = CHSV( HUE_PURPLE, 255, 255);
CRGB green = CHSV( HUE_GREEN, 255, 255);
CRGB black = CRGB::Black;
currentPalette = CRGBPalette16(
green, green, black, black,
purple, purple, black, black,
green, green, black, black,
purple, purple, black, black );
}
/*
// 此示例说明如何设置静态调色板
// 存储在PROGMEM(闪存)中,几乎总是
// 比RAM丰富。 像这样的静态PROGMEM调色板
// 占用64个字节的Flash。
*/
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
CRGB::Red,
CRGB::Gray, // 与红色和蓝色相比,“白色”太亮
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Gray,
CRGB::Blue,
CRGB::Black,
CRGB::Red,
CRGB::Red,
CRGB::Gray,
CRGB::Gray,
CRGB::Blue,
CRGB::Blue,
CRGB::Black,
CRGB::Black
};