// 将第二条灯带的所有 LED 设置为黄色 for (int i = 0; i < LED_COUNT_2; i++) { leds_2[i] = CRGB(255, 255, 0); } // 每隔2秒增加亮度 static unsigned long lastUpdateTime = 0; unsigned long currentTime = millis(); if (currentTime - lastUpdateTime >= 2000) { lastUpdateTime = currentTime; brightness2 += 10; if (brightness2 > 255) { brightness2 = 0; } } // 设置第二条灯带的亮度 FastLED.setBrightness(BRIGHTNESS_2); // 更新 LED 灯带 FastLED.show();亮度增加不明显
时间: 2024-03-17 15:45:34 浏览: 21
如果您发现亮度增加不明显,可能是因为每次亮度只增加了10,而且只在每隔2秒时增加一次。您可以尝试增加亮度增加的步长,或者缩短增加亮度的时间间隔,以使亮度变化更明显。
例如,您可以将每次亮度增加的步长从10增加到20,或将时间间隔从2秒缩短到1秒。这样,每次亮度增加的变化就更加明显了。
以下是一个示例代码,其中亮度增加的步长为20,时间间隔为1秒:
```
// 将第二条灯带的所有 LED 设置为黄色
for (int i = 0; i < LED_COUNT_2; i++) {
leds_2[i] = CRGB(255, 255, 0);
}
// 每隔1秒增加亮度
static unsigned long lastUpdateTime = 0;
unsigned long currentTime = millis();
if (currentTime - lastUpdateTime >= 1000) {
lastUpdateTime = currentTime;
brightness2 += 20;
if (brightness2 > 255) {
brightness2 = 0;
}
}
// 设置第二条灯带的亮度
FastLED.setBrightness(BRIGHTNESS_2);
// 更新 LED 灯带
FastLED.show();
```
请注意,增加亮度的步长和时间间隔应根据您的具体需求进行调整。
相关问题
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秒增加亮度
可以将代码修改为:
```
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 灯带。最后,我们添加了一个延迟,避免频繁更新亮度。
#include <FastLED.h> #define LED_PIN_1 3 #define LED_PIN_2 6 #define LED_COUNT_1 53 #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]; int pin_switch = 10; // 定义数字输入口pin10接收开关状态 int switch_state = 0; // 定义开关状态变量 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); Serial.begin(9600);//设置串口波特率为9600kbps pinMode(pin_switch, INPUT); // 初始化数字输入口 } void loop() { switch_state = digitalRead(pin_switch); // 读取开关状态 // 开关按下 if (switch_state == LOW) { // 闪烁两次黄光 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); }第一条灯带所有灯珠在闪烁完熄灭之后,慢慢亮为绿色
要在第一条灯带所有灯珠在闪烁完熄灭之后,慢慢亮为绿色,可以在第13行闪烁完熄灭之后,添加一个循环逐渐将所有灯珠从黑色变为绿色,代码如下:
```
// 闪烁两次黄光
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);
// 逐渐将所有灯珠从黑色变为绿色
for (int k = 0; k < LED_COUNT_1; k++) {
leds_1[k] = CRGB::Green;
FastLED.show();
delay(20);
}
}
```