for (int i = 0; i < LED_COUNT_2; i++) { leds_2[i] = CRGB(255, 255, 0); // 将第二条灯带的所有 LED 设置为黄色 } FastLED.setBrightness(brightness2); // 设置第二条灯带的亮度 FastLED.show(); // 更新 LED 灯带 // 每隔一段时间增加或减小亮度 brightness2 = brightness2 + 10; // 增加亮度 if (brightness2 > 255) { brightness2 = 0; // 如果亮度超过 255,则将其设置为最小值 0 } delay(100000); // 延迟一秒钟 这段代码改为每隔5秒增加亮度
时间: 2024-03-17 13:45:14 浏览: 123
可以将代码修改为:
```
void loop() {
// 将第二条灯带的所有 LED 设置为黄色
for (int i = 0; i < LED_COUNT_2; i++) {
leds_2[i] = CRGB(255, 255, 0);
}
// 每隔5秒增加亮度
static unsigned long lastUpdateTime = 0;
unsigned long currentTime = millis();
if (currentTime - lastUpdateTime >= 5000) {
lastUpdateTime = currentTime;
brightness2 += 10;
if (brightness2 > 255) {
brightness2 = 0;
}
}
// 设置第二条灯带的亮度
FastLED.setBrightness(brightness2);
// 更新 LED 灯带
FastLED.show();
delay(100); // 延迟一段时间,避免频繁更新亮度
}
```
这段代码中,我们使用了一个静态变量 `lastUpdateTime` 记录上一次亮度更新的时间,每隔 5 秒钟增加亮度。然后,我们设置第二条灯带的亮度,并更新 LED 灯带。最后,我们添加了一个延迟,避免频繁更新亮度。
相关问题
// 闪烁两次黄光 for (int i = 0; i < 3; i++) { // 点亮所有灯珠,颜色为黄色 for (int j = 0; j < LED_COUNT_1; j++) { leds_1[j] = CRGB::Yellow; } FastLED.show(); delay(200); // 熄灭所有灯珠 for (int j = 0; j < LED_COUNT_1; j++) { leds_1[j] = CRGB::Black; } FastLED.show(); delay(200); }灯光颜色改为橘色
要将上述代码中的灯光颜色改为橘色,只需要在第6行将CRGB::Yellow改为CRGB::Orange即可。修改后的代码如下:
```
// 闪烁两次橘光
for (int i = 0; i < 3; i++) {
// 点亮所有灯珠,颜色为橘色
for (int j = 0; j < LED_COUNT_1; j++) {
leds_1[j] = CRGB::Orange;
}
FastLED.show();
delay(200);
// 熄灭所有灯珠
for (int j = 0; j < LED_COUNT_1; j++) {
leds_1[j] = CRGB::Black;
}
FastLED.show();
delay(200);
}
```
#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); } 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_2 >= 0) { 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循环中,每次让第二条灯带的亮度增加一点,如下所示:
```
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);
// 让第二条灯带的亮度增加1
for (int i = 0; i < LED_COUNT_2; i++) {
leds_2[i].fadeLightBy(1);
}
}
```
这样,第二条灯带的亮度就会慢慢增加,同时第一条灯带会呼吸。你可以根据需要调整第二条灯带亮度增加的速度,即调整第二个for循环中的fadeLightBy()函数的参数。
阅读全文