#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后,第一条灯带也不亮了
时间: 2023-08-05 17:39:34 浏览: 47
如果将BRIGHTNESS_2调整为0后,第一条灯带也不亮了,这可能是由于FastLED.setBrightness()函数被用于设置所有灯带的亮度。这导致第一条灯带的亮度也被设置为0,因此它不再亮起来。
为了解决这个问题,您可以使用if else语句来分别设置每个灯带的亮度。例如,您可以使用以下代码来设置第二条灯带的亮度为0,同时保持第一条灯带的亮度为255:
```
// 设置第一条灯带的亮度
FastLED.setBrightness(BRIGHTNESS);
// 设置第二条灯带的亮度
if (brightness2 > 0) {
FastLED.setBrightness(BRIGHTNESS_2);
} else {
FastLED.setBrightness(0);
}
// 更新 LED 灯带
FastLED.show();
```
这样,只有在brightness2大于0时才会设置第二条灯带的亮度,否则将设置所有灯带的亮度为0。
相关问题
#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);
}
```
这将使第一条灯带变为红色,第二条灯带变为蓝色,然后重复此过程。您可以根据需要更改颜色和时间间隔。
#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灯带的控制,分别连接在3和6号引脚上。
* 第一个LED灯带有42个灯珠,第二个LED灯带有7个灯珠。
* 灯珠类型为WS2812B,颜色排序方式为GRB。
* 设置亮度为255。
*/
#include <FastLED.h>
#define LED_PIN_1 3 //第一个LED灯带连接的引脚
#define LED_PIN_2 6 //第二个LED灯带连接的引脚
#define LED_COUNT_1 42 //第一个LED灯带的LED数量
#define LED_COUNT_2 7 //第二个LED灯带的LED数量
#define BRIGHTNESS 255 //灯光亮度
#define LED_TYPE WS2812B //灯珠类型
#define COLOR_ORDER GRB //颜色排序方式
CRGB leds_1[LED_COUNT_1]; //第一个LED灯带的LED数组
CRGB leds_2[LED_COUNT_2]; //第二个LED灯带的LED数组
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN_1, COLOR_ORDER>(leds_1, LED_COUNT_1); //连接第一个LED灯带
FastLED.addLeds<LED_TYPE, LED_PIN_2, COLOR_ORDER>(leds_2, LED_COUNT_2); //连接第二个LED灯带
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); //延时控制
}
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-dosexec](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)