#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) {开关按下之后闪烁两次黄光
时间: 2024-03-22 07:38:51 浏览: 39
好的,您可以使用下面这段代码实现开关按下之后闪烁两次黄光的功能,其中第一组灯珠闪烁黄光 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 个灯珠。同样,您可以根据实际需要调整延时时间和颜色。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.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)