#include <FastLED.h> #define LED_PIN 3 #define LED_COUNT 42 #define BRIGHTNESS 255 #define LED_TYPE WS2812B #define COLOR_ORDER GRB CRGB leds[LED_COUNT]; void setup() { FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, LED_COUNT); FastLED.setBrightness(BRIGHTNESS); } void loop() { int brightness = 0; int fadeAmount = 5; while (brightness <= 255) { for (int i = 0; i < LED_COUNT; i++) { leds[i].setRGB(brightness, brightness, brightness); } FastLED.show(); brightness += fadeAmount; delay(25); } while (brightness >= 0) { for (int i = 0; i < LED_COUNT; i++) { leds[i].setRGB(brightness, brightness, brightness); } FastLED.show(); brightness -= fadeAmount; delay(25); } }增加第二条灯带,灯带颜色为黄色,并且一开始是灭的,然后一点一点变亮
时间: 2024-03-17 10:45:54 浏览: 106
可以将代码修改如下:
```
#include <FastLED.h>
#define LED_PIN_1 3
#define LED_PIN_2 5
#define LED_COUNT_1 42
#define LED_COUNT_2 42
#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_1 = 0;
int brightness_2 = 0;
int fadeAmount = 5;
while (brightness_1 <= 255) {
for (int i = 0; i < LED_COUNT_1; i++) {
leds_1[i].setRGB(brightness_1, brightness_1, brightness_1);
}
FastLED.show();
brightness_1 += fadeAmount;
delay(25);
}
while (brightness_2 <= 255) {
for (int i = 0; i < LED_COUNT_2; i++) {
leds_2[i].setRGB(255, 255, 0);
leds_2[i].fadeLightBy(brightness_2);
}
FastLED.show();
brightness_2 += fadeAmount;
delay(25);
}
while (brightness_1 >= 0) {
for (int i = 0; i < LED_COUNT_1; i++) {
leds_1[i].setRGB(brightness_1, brightness_1, brightness_1);
}
FastLED.show();
brightness_1 -= fadeAmount;
delay(25);
}
}
```
这个代码会将第一条灯带一点一点变亮,然后第二条灯带从灭的状态开始,一点一点变成黄色并且变亮,最后第一条灯带再一点一点变暗。注意,需要在代码中添加第二条灯带的引脚和数量,并且使用 `fadeLightBy()` 函数来让灯带一点一点变亮。
阅读全文