#include <FastLED.h> #define LED_PIN 3 #define LED_PIN_2 6 // 定义第二条灯带的引脚 #define LED_COUNT 42 #define LED_COUNT_2 7 // 定义第二条灯带的 LED 数量 #define BRIGHTNESS 255 #define BRIGHTNESS_2 255 #define LED_TYPE WS2812B #define COLOR_ORDER GRB CRGB leds[LED_COUNT]; CRGB leds_2[LED_COUNT_2]; // 定义第二条灯带的 LED 数组 int brightness2 = 0; // 定义初始亮度为 100 void setup() { FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, LED_COUNT); FastLED.addLeds<LED_TYPE, LED_PIN_2, COLOR_ORDER>(leds_2, LED_COUNT_2); // 添加第二条灯带 FastLED.setBrightness(BRIGHTNESS); FastLED.setBrightness(BRIGHTNESS_2); } void loop() { /太阳灯带**/ // 将第二条灯带的所有 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 >= 1000) { lastUpdateTime = currentTime; brightness2 -= 10; if (brightness2 > 255) { brightness2 = 0; } } // 设置第二条灯带的亮度 FastLED.setBrightness(BRIGHTNESS_2); // 更新 LED 灯带 FastLED.show(); }为什么第二条灯带亮度没有变化
时间: 2024-03-17 13:45:44 浏览: 21
根据你提供的代码,第二条灯带的亮度没有变化是因为在循环中虽然有修改亮度变量brightness2的代码,但是并没有将修改后的brightness2的值赋给第二条灯带的亮度。
你可以尝试在循环中添加以下代码来将修改后的亮度值赋给第二条灯带:
```
FastLED.setBrightness(brightness2);
```
这样就可以根据brightness2的值来控制第二条灯带的亮度了。
相关问题
#include <FastLED.h> #define LED_PIN 3 #define LED_PIN_2 6 // 定义第二条灯带的引脚 #define LED_COUNT 42 #define LED_COUNT_2 7 // 定义第二条灯带的 LED 数量 #define BRIGHTNESS 255 #define BRIGHTNESS_2 0 #define LED_TYPE WS2812B #define COLOR_ORDER GRB为什么调整了BRIGHTNESS_2为0后,第一条灯带也不亮了
如果将BRIGHTNESS_2设置为0后,第一条灯带也不亮了,那可能是因为您在设置第二条灯带的亮度时没有考虑到第一条灯带的亮度。
您可以尝试修改代码,首先设置第一条灯带的亮度,然后再根据brightness2的值来设置第二条灯带的亮度。如下所示:
```
// 设置第一条灯带的亮度
FastLED.setBrightness(BRIGHTNESS);
// 设置第二条灯带的亮度
if (brightness2 > 0) {
FastLED.setBrightness(BRIGHTNESS_2);
} else {
FastLED.setBrightness(BRIGHTNESS);
}
// 更新 LED 灯带
FastLED.show();
```
这样,如果brightness2大于0,则设置第二条灯带的亮度为BRIGHTNESS_2,否则设置第二条灯带的亮度为第一条灯带的亮度。然后,更新所有的LED灯带。
#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); }增加另一条灯带
您可以使用以下步骤来添加另一条灯带:
1.确定您将使用的灯带的数量和类型,以及它们所需的引脚。
2.在您的代码中定义第二个灯带的LED数量和引脚,以及要用于该灯带的FastLED控制器。例如:
```
#define LED_PIN_1 3
#define LED_COUNT_1 42
#define LED_PIN_2 5
#define LED_COUNT_2 30
CRGB leds_1[LED_COUNT_1];
CRGB leds_2[LED_COUNT_2];
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);
```
3.编写代码以控制两个灯带。您可以使用类似以下代码的函数来控制每个灯带:
```
void setLeds1Color(CRGB color) {
// Set the color of the LEDs in the first strip
for (int i = 0; i < LED_COUNT_1; i++) {
leds_1[i] = color;
}
// Show the updated colors on the LEDs
FastLED.show();
}
void setLeds2Color(CRGB color) {
// Set the color of the LEDs in the second strip
for (int i = 0; i < LED_COUNT_2; i++) {
leds_2[i] = color;
}
// Show the updated colors on the LEDs
FastLED.show();
}
```
4.在您的主循环中,调用这些函数以更改灯带的颜色:
```
void loop() {
// Set the color of the first strip to red
setLeds1Color(CRGB::Red);
// Wait for 1 second
delay(1000);
// Set the color of the second strip to blue
setLeds2Color(CRGB::Blue);
// Wait for 1 second
delay(1000);
}
```
这将使第一条灯带变为红色,第二条灯带变为蓝色,然后重复此过程。您可以根据需要更改颜色和时间间隔。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)