switch_state = digitalRead(pin_switch); // 读取开关状态 // 开关按下 if (switch_state == LOW) {开关按下之后闪烁两次黄色灯光
时间: 2024-03-22 18:38:50 浏览: 20
您可以尝试使用下面这段代码实现开关按下之后闪烁两次黄色灯光的功能:
```
// 读取开关状态
switch_state = digitalRead(pin_switch);
// 开关按下
if (switch_state == LOW) {
// 闪烁两次黄色灯光
for (int i = 0; i < 2; i++) {
// 点亮黄色灯光
setPixelColor(0, yellow);
show();
delay(500);
// 熄灭灯光
setPixelColor(0, black);
show();
delay(500);
}
}
```
其中,`setPixelColor(0, yellow)` 用于点亮第一个灯珠(假设只有一个灯珠),颜色为黄色;`setPixelColor(0, black)` 用于熄灭第一个灯珠。`delay(500)` 表示延时 500 毫秒。您可以根据实际需要调整延时时间和颜色。
相关问题
#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_switch = 10; // 定义数字输入口pin10接收开关状态 int switch_state = 0; // 定义开关状态变量 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 pinMode(pin_switch, INPUT); // 初始化数字输入口 } void loop() { switch_state = digitalRead(pin_switch); // 读取开关状态 // 开关按下 if (switch_state == LOW) {开关按下之后闪烁两次黄光
好的,您可以使用下面这段代码实现开关按下之后闪烁两次黄光的功能,其中第一组灯珠闪烁黄光 2 次,第二组灯珠闪烁黄光 1 次:
```
// 读取开关状态
switch_state = digitalRead(pin_switch);
// 开关按下
if (switch_state == LOW) {
// 第一组灯珠闪烁黄光 2 次
for (int i = 0; i < 2; i++) {
// 点亮第一组灯珠,颜色为黄色
for (int j = 0; j < LED_COUNT_1; j++) {
leds_1[j] = CRGB::Yellow;
}
FastLED.show();
delay(500);
// 熄灭第一组灯珠
for (int j = 0; j < LED_COUNT_1; j++) {
leds_1[j] = CRGB::Black;
}
FastLED.show();
delay(500);
}
// 第二组灯珠闪烁黄光 1 次
// 点亮第二组灯珠,颜色为黄色
for (int j = 0; j < LED_COUNT_2; j++) {
leds_2[j] = CRGB::Yellow;
}
FastLED.show();
delay(500);
// 熄灭第二组灯珠
for (int j = 0; j < LED_COUNT_2; j++) {
leds_2[j] = CRGB::Black;
}
FastLED.show();
}
```
其中,`leds_1` 和 `leds_2` 分别表示第一组和第二组灯珠的数组。`leds_1[j] = CRGB::Yellow` 用于点亮第 j 个灯珠,颜色为黄色;`leds_1[j] = CRGB::Black` 用于熄灭第 j 个灯珠。同样,您可以根据实际需要调整延时时间和颜色。
#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_switch = 10; // 定义数字输入口pin10接收开关状态 int switch_state = 0; // 定义开关状态变量 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 pinMode(pin_switch, INPUT); // 初始化数字输入口 } void loop() { switch_state = digitalRead(pin_switch); // 读取开关状态 if (switch_state == LOW) { // 开关按下,灯带状态为白色流水灯 for (int i = 0; i < LED_COUNT_1; i++ ) { leds_1[i] = CRGB::White; } FastLED.show(); delay(25); } else { // 开关未按下,灯带状态为绿色呼吸灯 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); } } }开关按下之后,不能立刻终止呼吸灯循环,怎么改
可以使用一个变量来记录开关状态,只有当开关状态变为未按下时,才退出呼吸灯循环。具体做法如下:
```
void loop() {
switch_state = digitalRead(pin_switch); // 读取开关状态
if (switch_state == LOW) {
// 开关按下,灯带状态为白色流水灯
for (int i = 0; i < LED_COUNT_1; i++ ) {
leds_1[i] = CRGB::White;
}
FastLED.show();
delay(25);
} else {
// 开关未按下,灯带状态为绿色呼吸灯
bool breathing = true; // 定义一个变量用于记录呼吸灯状态
while (breathing) { // 当呼吸灯状态为true时,执行循环
int brightness = 0;
for (int i = 0; i < 128; i++) {
brightness = (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, 0);
}
FastLED.show();
delay(10);
switch_state = digitalRead(pin_switch);
if (switch_state == LOW) { // 如果开关被按下,退出循环
breathing = false;
break;
}
}
}
}
}
```
在呼吸灯循环中,添加一个while循环,当开关被按下时,将呼吸灯状态变为false,退出循环。同时,每次循环前读取一次开关状态,如果开关被按下,也退出循环。这样可以保证在开关被按下后,呼吸灯循环再执行完毕后才退出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)