如何使用 Arduino 在 LED 灯条上创建彩虹波?

How to create a rainbow wave on LED Strip using Arduino?

我想用我的arduino nano作为控制器为我的led灯带创造一些效果。

到目前为止,我设法完成了基础工作(每个 LED 的静态颜色相同,每个 LED 的颜色同时褪色)。 我得到了彩虹效果,但它基本上只是同时通过所有 LED 的色谱循环。

我想要的是彩虹波,其中颜色向一个方向移动并相互淡化into/chasing。

我假设你想要这样的东西:

我正在为此使用 FastLED 库,但我认为您可以稍微更改代码以使其适用于不同的 LED 库。

#include <FastLED.h>

#define NUM_LEDS 60      /* The amount of pixels/leds you have */
#define DATA_PIN 7       /* The pin your data line is connected to */
#define LED_TYPE WS2812B /* I assume you have WS2812B leds, if not just change it to whatever you have */
#define BRIGHTNESS 255   /* Control the brightness of your leds */
#define SATURATION 255   /* Control the saturation of your leds */

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  for (int j = 0; j < 255; j++) {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(i - (j * 2), SATURATION, BRIGHTNESS); /* The higher the value 4 the less fade there is and vice versa */ 
    }
    FastLED.show();
    delay(25); /* Change this to your hearts desire, the lower the value the faster your colors move (and vice versa) */
  }
}

你试过 fill_rainbow 来自 FastLED 图书馆吗?未经测试但应该可以工作。

#include "FastLED.h"                                     

#define LED_DT 1                                     

#define COLOR_ORDER GRB                                       
#define LED_TYPE WS2812                                       
#define NUM_LEDS 15                                   
 
uint8_t max_bright = 255;                         
 
struct CRGB leds[NUM_LEDS]; 
 
void setup() {
 
  Serial.begin(115200);
 
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);      
 
  FastLED.setBrightness(max_bright);         
 
} // setup()
 
 
 
void loop () {
    uint8_t thisSpeed = 10;
    uint8_t deltaHue= 10;
    uint8_t thisHue = beat8(thisSpeed,255); 
    fill_rainbow(leds, NUM_LEDS, thisHue, deltaHue);            
    FastLED.show();
}
 

同样的假设,不同的方法:你想要这样的东西(喜欢这个 gif):

它与最新的 FastLED 库一起工作 'onboard' 具有更均匀的时间和平滑的发现 here:

#include <FastLED.h>

#define NUM_LEDS  18
#define LED_PIN   2

CRGB leds[NUM_LEDS];

//has to be uint8_t so it starts at 0 after it reached 256
uint8_t hue = 0;

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

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

  //You can change the pattern speed here
  EVERY_N_MILLISECONDS(15){
    hue++;
  }
  
  FastLED.show();
}

玩得开心 :)