#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); } delay(2000); 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 12:40:50 浏览: 43
C语言编嵌入式开发笔记-以led控制程序为例.md
这段代码的错误在于,在第二个while循环中,`delay(25)`应该放在`brightness -= fadeAmount;`语句的后面,而不是前面。这会导致第二个while循环中的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);
}
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);
}
delay(2000);
while (brightness >= 0) {
for (int i = 0; i < LED_COUNT; i++) {
leds[i].setRGB(brightness, brightness, brightness);
}
FastLED.show();
brightness -= fadeAmount;
delay(25);
}
}
```
现在,第二个while循环中的LED灯应该逐渐变暗,而不会出现闪烁或暂停的情况。
阅读全文