#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); } 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 13:41:02 浏览: 128
C语言编嵌入式开发笔记-以led控制程序为例.md
要将上述代码中的颜色改为绿色,只需要将 `leds[i].setRGB(brightness, brightness, brightness);` 中的三个参数改为 `0, brightness, 0`,这将会使 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(0, brightness, 0);
}
FastLED.show();
brightness += fadeAmount;
delay(25);
}
while (brightness >= 0) {
for (int i = 0; i < LED_COUNT; i++) {
leds[i].setRGB(0, brightness, 0);
}
FastLED.show();
brightness -= fadeAmount;
delay(25);
}
}
```
请注意,这只是将 LED 的颜色改为了绿色,并没有改变代码的其他功能。
阅读全文