#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 = 10; //定义数字检测接口pin0 int val;//定义变量 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 } void loop() { int brightness_2 = 0; int fadeAmount = 5; val = digitalRead(pin); //读取模拟接口的值 Serial.println(val);//输出模拟接口的值 if (val == 0) //如果Pin10检测的值为高电平,点亮LED { for (int i = 0; i < LED_COUNT_1; i++ ) { leds_1[i] = CRGB::White; FastLED.show(); delay(25); } } else //如果为低电平熄灭LED { for (int i = 0; i < 128; 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(0, brightness_1, 0); } FastLED.show(); delay(10); } } }呼吸灯循环怎么设置为动态循环
时间: 2024-03-21 16:44:33 浏览: 114
linux_s3c2410_gpio.rar_GPIO_GPIO linux _gpio 2410_linux gpio_s3c
要将呼吸灯循环设置为动态循环,可以使用一个循环计数器变量来控制呼吸灯的亮度变化。具体地,可以在循环开始前定义一个计数器变量,然后在循环中根据计数器变量的值计算亮度,接着让计数器变量递增或递减,以控制呼吸灯的亮度变化。
以下是一个示例代码,其中使用了一个循环计数器变量 i,每次循环让 i 递增或递减,用 sin 函数计算亮度,并将亮度值赋给 brightness_1 变量,最后将 brightness_1 赋值给所有 LED 的亮度值。这样就可以实现动态的呼吸灯效果。
```
void loop() {
int brightness_2 = 0;
int fadeAmount = 5;
val = digitalRead(pin); //读取模拟接口的值
Serial.println(val);//输出模拟接口的值
if (val == 0) { //如果Pin10检测的值为高电平,点亮LED
for (int i = 0; i < LED_COUNT_1; i++ ) {
leds_1[i] = CRGB::White;
FastLED.show();
delay(25);
}
} else { //如果为低电平熄灭LED
int i = 0;
while (true) {
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(0, brightness_1, 0);
}
FastLED.show();
delay(10);
i++;
if (i >= 100) {
i = 0;
}
}
}
}
```
阅读全文