fastled教程

效果预览
这是色板这一的例子 从左到右分别为:
3-1 3-2 3-3 三个例子
请添加图片描述

EVERY_N_MILLISECONDS(10)
EVERY_N_SECONDS(5)
EVERY_N_MILLISECONDS(10);
如 
EVERY_N_SECONDS(5) {
    whichPalette++;
    if (whichPalette > 2) whichPalette = 0;
    Serial.println(currentPalette[0]);
  }

fill_solid(leds, NUM_LEDS, CRGB::Red);
fill_solid(leds, NUM_LEDS, CRGB::Red);
  FastLED.show();
fill_gradient_RGB

在这里插入图片描述

CRGB startColor = CRGB::Red; 
CRGB endColor = CRGB::Blue;

 
fill_gradient_RGB(leds, 0, startColor, NUM_LEDS / 2, endColor ); //从第 0 个LED到第 NUM_LEDS/2个LED,颜色从红到蓝
fill_gradient_RGB(leds, NUM_LEDS / 2, endColor, NUM_LEDS - 1, startColor ); //从第 NUM_LEDS/2 起,颜色从蓝到红
FastLED.show();
fill_rainbow(leds, NUM_LEDS, i, 255 / NUM_LEDS);

fill_rainbow 是 FastLED 库中的一个函数,用于在 LED 灯带或矩阵上以彩虹色填充颜色。

fill_rainbow(leds, NUM_LEDS, beginHue, deltaHue); 

beginHue++;在loop中不断循环,这样就能看到:
在这里插入图片描述


uint8_t beginHue;
void loop() {
  // put your main code here, to run repeatedly:
  beginHue++;
  fill_rainbow(myled,30,beginHue,9);
  FastLED.show();
  delay(25);//delay不能很大,很大效果会生硬

}

效果1
fadeToBlackBy(leds, NUM_LEDS, 1);

在这里插入图片描述

#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(57600);
}

void loop() {

for(int i=0;i<14;i++){
  uint16_t sinBeat = beatsin16(30, 0, NUM_LEDS - 1, 0, 0);

  leds[sinBeat ] = CRGB::Red;
   
  
 fadeToBlackBy(leds, NUM_LEDS, 1);

  FastLED.show();}
  
}
效果2
FastLED.setBrightness(2*i);//

也是逐个点亮,并且有个拖尾
在这里插入图片描述

#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(57600);
}

void loop() {


  for (int i=0; i<NUM_LEDS; i++){
    leds[i] = CRGB(0, 255 - 4*i, 4*i );
    FastLED.setBrightness(2*i);//
    FastLED.show();
    delay(50);
      
  
}FastLED.clear();}
效果3
leds[i] = CHSV(hue + (i * 10), 255, 255);

在这里插入图片描述

#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8
int hue=0;
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(57600);
}
void loop() {

for (int i = 0; i < NUM_LEDS; i++) {
    //leds[i] = CHSV(hue, 255, 255);
 leds[i] = CHSV(hue + (i * 10), 255, 255);
}

delay(15);
hue++;


 FastLED.show();
}
效果4
复制别的LED颜色 leds[i] = leds[i - 1];

在这里插入图片描述

#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8
int hue=0;
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(57600);
}
void loop() {
effect3();
}

void effect3() {

  delay(50) ;{
    
    // Create a new HSV color for led[0]
    leds[0] = CHSV(160, random8(), random8(100, 255));
    
    // 把上一个颜色值复制过来Copy each pixel to the next one, starting at the far end
    // thereby 'moving' the pattern along the strip
    for (int i = NUM_LEDS - 1; i > 0; i--) {
      leds[i] = leds[i - 1];
    }
  }
  
  FastLED.show();
}

  
  FastLED.show();
}
3-4 fadeToBlackBy(leds, NUM_LEDS, 10);

效果:每隔5s,逐个点亮LED,并有个拖尾;
fadeToBlackBy(leds, NUM_LEDS, 50);最后一个参数越大,拖尾越短

在这里插入图片描述

void loop(){
   for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB::Red;
   fadeToBlackBy(leds, NUM_LEDS, 50);
    FastLED.show();
    delay(50);
    } 
    //FastLED.clear(); //这个Fastled.clear不应该加的,加了会直接让灯灭了,效果会生硬
 
for(int i = 0; i < NUM_LEDS; i++) {
    leds[NUM_LEDS-i] = CRGB::Blue;
   fadeToBlackBy(leds, NUM_LEDS, 10);
    FastLED.show();
    delay(50);
    } 
    
}
————————————————————————————————————————————

3 色板

这是从哔哩哔哩教程总结的
在这里插入图片描述

色板工具_______

工具1:渐变色生成工具 PaletteKnife for FastLED
1 工具地址

http://fastled.io/tools/paletteknife/

2.操作步骤

请添加图片描述

3.程序模版
 3-1 把例子中的 : //-----1------内容替换
 3-2  把  **Sunset_Real_gp**  替换掉就行了
#include <FastLED.h>

#define NUM_LEDS  18
#define LED_PIN   2

CRGB leds[NUM_LEDS];

uint8_t paletteIndex = 0;
//------------------------------------------------------------------------------1--------------------------------------------------------------------------------------------------
// Gradient palette "Sunset_Real_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/nd/atmospheric/tn/Sunset_Real.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 28 bytes of program space.

DEFINE_GRADIENT_PALETTE( Sunset_Real_gp ) {
    0, 120,  0,  0,
   22, 179, 22,  0,
   51, 255,104,  0,
   85, 167, 22, 18,
  135, 100,  0,103,
  198,  16,  0,130,
  255,   0,  0,160};
//------------------------------------------------------------------------------1--------------------------------------------------------------------------------------------------

CRGBPalette16 myPal = Sunset_Real_gp;

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
}

void loop() {
  fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, myPal, 255, LINEARBLEND);
  
  EVERY_N_MILLISECONDS(10){
    paletteIndex++;
  }
  
  FastLED.show();
}
工具2:cssgradient 工具

工具地址:

https://cssgradient.io/

在这里插入图片描述

3-1 CRGBPalette16
 fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, myPal, 255, LINEARBLEND);
 fill_palette(leds, NUM_LEDS, 开始的位置, 相邻颜色变化是多少, 调色板名字, 亮度, 融合类型;

在这里插入图片描述

在这里插入图片描述

#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8

CRGB leds[NUM_LEDS];

uint8_t paletteIndex = 0;
//这个调色板,只能写16个颜色  ,多了不能写
//调色板如上图所示
CRGBPalette16 purplePalette = CRGBPalette16 (
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    
    CRGB::Magenta,
    CRGB::Magenta,
    CRGB::Linen,
    CRGB::Linen,
    
    CRGB::Magenta,
    CRGB::Magenta,
    CRGB::DarkViolet,
    CRGB::DarkViolet,

    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::Linen,
    CRGB::Linen
);

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
}

void loop() {
  fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, purplePalette, 255, LINEARBLEND);
  
  EVERY_N_MILLISECONDS(10){
    paletteIndex++;
  }
  
  FastLED.show();
}
3-2 CRGBPalette16 进阶

在这里插入图片描述

#include <FastLED.h>

#define NUM_LEDS  18
#define LED_PIN   2

CRGB leds[NUM_LEDS];

uint8_t paletteIndex = 0;

CRGBPalette16 purplePalette = CRGBPalette16 (
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::DarkViolet,
    
    CRGB::Magenta,
    CRGB::Magenta,
    CRGB::Linen,
    CRGB::Linen,
    
    CRGB::Magenta,
    CRGB::Magenta,
    CRGB::DarkViolet,
    CRGB::DarkViolet,

    CRGB::DarkViolet,
    CRGB::DarkViolet,
    CRGB::Linen,
    CRGB::Linen
);


CRGBPalette16 myPal = purplePalette;

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
}

void loop() {
  
  EVERY_N_MILLISECONDS(50){
    //Switch on an LED at random, choosing a random color from the palette
    leds[random8(0, NUM_LEDS - 1)] = ColorFromPalette(myPal, random8(), 255, LINEARBLEND);
  }

   // Fade all LEDs down by 1 in brightness each time this is called
   fadeToBlackBy(leds, NUM_LEDS, 1);
  
  FastLED.show();
}
3-3 heatmap_gp 另一种定义色板的方式 ,据说是比较新的!(效果6)

在这里插入图片描述

这个可以增加好多索引

DEFINE_GRADIENT_PALETTE (heatmap_gp) {
    0,   0,   0,   0,   //black
  128, 255,   0,   0,   //red

  150, 255,   125,   0,   //新加的

  200, 255, 255,   0,   //bright yellow
  255, 255, 255, 255    //full white 
};
#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8

CRGB leds[NUM_LEDS];

uint8_t paletteIndex = 0;

DEFINE_GRADIENT_PALETTE (heatmap_gp) {
    0,   0,   0,   0,   //black
  128, 255,   0,   0,   //red
  200, 255, 255,   0,   //bright yellow
  255, 255, 255, 255    //full white 
};

CRGBPalette16 myPal = heatmap_gp;//这个名字是可以变的heatmap_gp1.。。等等

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
}

void loop() {
  fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, myPal, 255, LINEARBLEND);
  
  EVERY_N_MILLISECONDS(10){
    paletteIndex++;
  }
  
  FastLED.show();
}
3-4 色板融合
nblendPaletteTowardPalette( currentPalette, targetPalette, 10 );

//nblendPaletteTowardPalette(currentPalette, targetPalette, amount);最后一参数是过渡量,越大过渡越快,!!!
//并且当前色板是变动的,如当融合后, 当前色板 由greenblue_gp---->browngreen_gp

#include <FastLED.h>

#define NUM_LEDS  14
#define LED_PIN   8

CRGB leds[NUM_LEDS];

uint8_t colorIndex[NUM_LEDS];
uint8_t whichPalette = 0;

DEFINE_GRADIENT_PALETTE( greenblue_gp ) { 
    0,    0,  194,  255,     //light blue
   46,    3,    0,  246,     //dark blue
  176,   55,  222,   70,     //bright green
  255,    0,  194,  255      //light blue
};

DEFINE_GRADIENT_PALETTE( orangepink_gp ) { 
    0,  255,  100,    0,     //orange
   90,  255,    0,  255,     //magenta
  150,  255,  100,    0,     //orange
  255,  255,  100,    0      //orange
};

DEFINE_GRADIENT_PALETTE( browngreen_gp ) { 
    0,    6,  255,    0,     //green
   71,    0,  255,  153,     //bluegreen
  122,  200,  200,  200,     //gray
  181,  110,   61,    6,     //brown
  255,    6,  255,    0      //green
};

CRGBPalette16 currentPalette(greenblue_gp);
CRGBPalette16 targetPalette(orangepink_gp);

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

  Serial.begin(57600);

  //Fill the colorIndex array with random numbers
  for (int i = 0; i < NUM_LEDS; i++) {
    colorIndex[i] = random8();
  }
}

void loop() {
  //首先是随机(colorIndex[i] = random8();在setup中设置了颜色的随机位置)点亮所有 LED
  
  // Color each pixel from the palette using the index from colorIndex[]
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette(currentPalette, colorIndex[i]);
  } 
//然后融合,
  nblendPaletteTowardPalette( currentPalette, targetPalette, 10 );//nblendPaletteTowardPalette(currentPalette, targetPalette, amount);最后一参数是过渡量,越大过渡越快,!!!
                                                                  //并且当前色板是变动的,如当融合后,  当前色板   由greenblue_gp---->browngreen_gp
  switch (whichPalette) {
    case 0:
      targetPalette = orangepink_gp;
      break;
    case 1:
      targetPalette = browngreen_gp;
      break;
     case 2:
      targetPalette = greenblue_gp;
      break;
  }
 // 5S切换一下色板
  EVERY_N_SECONDS(5) {
    whichPalette++;
    if (whichPalette > 2) whichPalette = 0;
    Serial.println(currentPalette[0]);
  }
   // 5ms  颜色的索引+1,然后再 leds[i] = ColorFromPalette(currentPalette, colorIndex[i]);这个函数中,更新颜色值,创建动态效果
  EVERY_N_MILLISECONDS(5){
    for (int i = 0; i < NUM_LEDS; i++) {
      colorIndex[i]++;
    }
  }

  FastLED.show();
}
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: FastLED是一个非常流行的开源LED控制库,它为各种平台和微控制器提供了简单且高效的LED驱动接口。而ESP32是一款强大的开源微控制器,以其高性能和丰富的功能而闻名。 FastLED库具有许多功能和特点,包括支持多种LED协议、丰富的色彩控制和动画效果、内置的图案生成器等。这使得开发者可以轻松地实现各种各样的LED应用,如彩虹灯、呼吸灯、音乐可视化等。 ESP32与FastLED库完美匹配,因为它具有强大的计算和内存性能,可以实现更复杂的LED效果。此外,ESP32板上的WS2812B LED也是FastLED库所支持的。开发者只需简单地安装FastLED库并编写代码,就可以利用ESP32的资源来控制LED灯带。 使用FastLED库和ESP32来控制LED带并不复杂。您只需要首先连接LED到ESP32的适当引脚,然后编写程序进行初始化和设置。代码中,您可以选择合适的LED控制协议、定义颜色和亮度,并选择所需的动画效果。最后,您只需调用适当的函数来显示和更新LED。 总的来说,FastLED与ESP32结合使用是一个非常强大和灵活的LED控制方案。无论是学习如何控制LED,还是进行更高级的LED项目开发FastLED库与ESP32是一个非常强大的组合。它们的结合为开发者提供了极大的便利性和创造性,让他们能够轻松实现各种令人惊叹的LED效果。 ### 回答2: FastLED是一个用于控制LED灯带和矩阵的库,适用于ESP32等微控制器平台。 在ESP32上使用FastLED库,我们可以通过简单的代码实现对LED灯带或矩阵的控制。首先,我们需要在Arduino IDE中安装FastLED库,并将其包含到项目中。 然后,我们需要定义用于控制LED灯带或矩阵的引脚。对于ESP32,我们可以选择任意数字引脚来连接灯带或矩阵。 接下来,我们可以设置LED灯带或矩阵的一些基本属性,如长度、类型和颜色编码方式。FastLED库支持多种不同类型的灯带和矩阵,包括WS2812、APA102和SK6812等。 一旦设置完基本属性,我们就可以使用FastLED库中的函数来控制LED灯带或矩阵的亮度、颜色和显示效果。例如,我们可以使用setBrightness()函数设置亮度,使用CRGB()函数设置颜色,使用show()函数将设置的颜色显示在LED灯带或矩阵上。 此外,FastLED库还提供了许多其他有用的功能,如闪烁、渐变和霓虹灯效果等。我们可以根据需要使用这些功能来创造出丰富多样的灯光效果。 总的来说,FastLED库使得在ESP32上控制LED灯带或矩阵变得简单而灵活。通过使用FastLED库,我们可以轻松地实现各种创意项目,如灯光艺术装置、音乐可视化效果等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值