#include <FastLED.h> #define LED_PIN_1 3 #define LED_PIN_2 6 #define LED_COUNT_1 42 #define LED_COUNT_2 7 #define BRIGHTNESS 255 #define LED_TYPE WS2812B #define COLOR_ORDER GRB CRGB leds_1[LED_COUNT_1]; CRGB leds_2[LED_COUNT_2]; void setup() { FastLED.addLeds<LED_TYPE, LED_PIN_1, COLOR_ORDER>(leds_1, LED_COUNT_1); FastLED.addLeds<LED_TYPE, LED_PIN_2, COLOR_ORDER>(leds_2, LED_COUNT_2); FastLED.setBrightness(BRIGHTNESS); } void loop() { int brightness_2 = 0; int fadeAmount = 5; for (int i = 0; i < 7680; i++) { int brightness_1 = (exp(sin(i / 50.0 * PI)) - 0.36787944) * 108.0; for (int j = 0; j < LED_COUNT_1; j++) { leds_1[j].setRGB(brightness_1, brightness_1, brightness_1); } FastLED.show(); delay(20); } }让第二个LED灯带在第一个LED灯带呼吸的状态下同时慢慢亮起
时间: 2024-03-18 17:38:30 浏览: 76
可以尝试在循环中添加对第二个LED灯带的控制,使其跟随第一个灯带的呼吸状态,并且慢慢亮起。具体代码如下:
```
#include <FastLED.h>
#define LED_PIN_1 3
#define LED_PIN_2 6
#define LED_COUNT_1 42
#define LED_COUNT_2 7
#define BRIGHTNESS 255
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds_1[LED_COUNT_1];
CRGB leds_2[LED_COUNT_2];
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN_1, COLOR_ORDER>(leds_1, LED_COUNT_1);
FastLED.addLeds<LED_TYPE, LED_PIN_2, COLOR_ORDER>(leds_2, LED_COUNT_2);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
int brightness_2 = 0;
int fadeAmount = 5;
for (int i = 0; i < 7680; i++) {
int brightness_1 = (exp(sin(i / 50.0 * PI)) - 0.36787944) * 108.0;
for (int j = 0; j < LED_COUNT_1; j++) {
leds_1[j].setRGB(brightness_1, brightness_1, brightness_1);
}
// 将第二个灯带的亮度设为第一个灯带的亮度
for (int k = 0; k < LED_COUNT_2; k++) {
leds_2[k].setRGB(brightness_1, brightness_1, brightness_1);
}
// 逐渐增加第二个灯带的亮度
if (brightness_2 < brightness_1) {
brightness_2 += fadeAmount;
if (brightness_2 > brightness_1) {
brightness_2 = brightness_1;
}
for (int k = 0; k < LED_COUNT_2; k++) {
leds_2[k].setRGB(brightness_2, brightness_2, brightness_2);
}
}
FastLED.show();
delay(20);
}
}
```
需要注意的是,这段代码中的呼吸效果是由第一个灯带控制的,第二个灯带只是跟随变化,如果需要更改呼吸效果,需要修改对第一个灯带的控制。
阅读全文