改变部分代码,当声音传感器的值是小于30时,灯带在2秒后呈流水灯样。#include <Adafruit_NeoPixel.h> #define PIN 6 // 灯带数据-Arduino引脚 #define NUMPIXELS 60 // 灯珠数量 #define MIC_PIN A0 // 声音传感器-Arduino引脚 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); void setup() { pixels.begin(); // 初始化 灯带 Serial.begin(9600); // 串口9600 } void loop() { int micValue = analogRead(MIC_PIN); // 读 声音传感器值 if(micValue > 30){ // 判断声音传感器的值是否大于30 int brightness = map(micValue, 0, 1023, 0, 255); // 将数值映射到0-255范围内 int a = random(0,255); int b = random(0,255); for(int i=0; i<NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.ColorHSV(a, b, brightness)); // 灯珠亮度 } pixels.show(); // 显示 } else{ pixels.clear(); // 关闭所有灯珠 pixels.show(); // 显示 } delay(50);//0.01秒 }
时间: 2024-01-18 22:02:28 浏览: 30
你可以将 else 语句中的代码改为以下内容,以实现当声音传感器的值小于 30 时,灯带在 2 秒后呈流水灯样:
```
else {
pixels.clear(); // 关闭所有灯珠
pixels.show(); // 显示
delay(2000); // 延迟 2 秒
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0)); // 灯珠颜色为红色
pixels.show(); // 显示
delay(50); // 延迟 0.05 秒
}
}
```
这段代码会在声音传感器的值小于 30 时,先将所有灯珠关闭,显示 2 秒后,再将灯带变为红色流水灯样。
相关问题
改变部分代码,当声音传感器的值是小于30时,灯带在2秒后重复呈流水灯样,大于30时停止流水灯代码并继续重复之前代码。。#include <Adafruit_NeoPixel.h> #define PIN 6 // 灯带数据-Arduino引脚 #define NUMPIXELS 60 // 灯珠数量 #define MIC_PIN A0 // 声音传感器-Arduino引脚 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); void setup() { pixels.begin(); // 初始化 灯带 Serial.begin(9600); // 串口9600 } void loop() { int micValue = analogRead(MIC_PIN); // 读 声音传感器值 if(micValue > 30){ // 判断声音传感器的值是否大于30 int brightness = map(micValue, 0, 1023, 0, 255); // 将数值映射到0-255范围内 int a = random(0,255); int b = random(0,255); for(int i=0; i<NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.ColorHSV(a, b, brightness)); // 灯珠亮度 } pixels.show(); // 显示 } else{ pixels.clear(); // 关闭所有灯珠 pixels.show(); // 显示 } delay(50);//0.01秒 }
以下是修改后的代码,可以实现您的需求:
```
#include <Adafruit_NeoPixel.h>
#define PIN 6 // 灯带数据-Arduino引脚
#define NUMPIXELS 60 // 灯珠数量
#define MIC_PIN A0 // 声音传感器-Arduino引脚
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long previousMillis = 0; // 上一次流水灯时间
int interval = 2000; // 流水灯间隔时间
bool isFlowing = false; // 是否正在流水灯
void setup() {
pixels.begin(); // 初始化 灯带
Serial.begin(9600); // 串口9600
}
void loop() {
int micValue = analogRead(MIC_PIN); // 读 声音传感器值
if (micValue > 30) { // 判断声音传感器的值是否大于30
int brightness = map(micValue, 0, 1023, 0, 255); // 将数值映射到0-255范围内
int a = random(0, 255);
int b = random(0, 255);
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.ColorHSV(a, b, brightness)); // 灯珠亮度
}
pixels.show(); // 显示
isFlowing = false; // 停止流水灯
}
else {
if (!isFlowing) { // 如果之前没有流水灯,则设置上一次流水灯时间
previousMillis = millis();
isFlowing = true;
}
unsigned long currentMillis = millis(); // 获取当前时间
// 判断是否到达流水灯时间间隔
if (currentMillis - previousMillis >= interval) {
// 流水灯效果
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 255)); // 蓝色
pixels.show(); // 显示
delay(50); // 每个灯珠之间的间隔
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // 关闭
}
pixels.show(); // 显示
isFlowing = false; // 停止流水灯
} else {
pixels.clear(); // 关闭所有灯珠
pixels.show(); // 显示
}
}
delay(50); // 0.05秒
}
```
这段代码将在声音传感器的值小于30时,等待2秒后重复呈流水灯样的效果;而当声音传感器的值大于30时,会停止流水灯效果并继续执行之前的代码。
改变部分代码,使灯带在声音传感器的值是小于30的2秒后,呈流水灯样。#include <Adafruit_NeoPixel.h> #define PIN 6 // 灯带数据-Arduino引脚 #define NUMPIXELS 60 // 灯珠数量 #define MIC_PIN A0 // 声音传感器-Arduino引脚 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); void setup() { pixels.begin(); // 初始化 灯带 Serial.begin(9600); // 串口9600 } void loop() { int micValue = analogRead(MIC_PIN); // 读 声音传感器值 if(micValue > 30){ // 判断声音传感器的值是否大于30 int brightness = map(micValue, 0, 1023, 0, 255); // 将数值映射到0-255范围内 int a = random(0,255); int b = random(0,255); for(int i=0; i<NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.ColorHSV(a, b, brightness)); // 灯珠亮度 } pixels.show(); // 显示 } else{ pixels.clear(); // 关闭所有灯珠 pixels.show(); // 显示 } delay(50);//0.01秒 }
修改后的代码如下:
```C++
#include <Adafruit_NeoPixel.h>
#define PIN 6 // 灯带数据-Arduino引脚
#define NUMPIXELS 60 // 灯珠数量
#define MIC_PIN A0 // 声音传感器-Arduino引脚
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long lastTriggerTime = 0;
bool isWaterfall = false;
void setup() {
pixels.begin(); // 初始化 灯带
Serial.begin(9600); // 串口9600
}
void loop() {
int micValue = analogRead(MIC_PIN); // 读 声音传感器值
if (micValue > 30) { // 判断声音传感器的值是否大于30
int brightness = map(micValue, 0, 1023, 0, 255); // 将数值映射到0-255范围内
int a = random(0, 255);
int b = random(0, 255);
if (isWaterfall) {
for (int i = NUMPIXELS - 1; i >= 0; i--) {
pixels.setPixelColor(i, pixels.ColorHSV(a, b, brightness)); // 灯珠亮度
pixels.show(); // 显示
delay(20); // 0.02秒
pixels.setPixelColor(i, 0); // 关闭灯珠
}
} else {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.ColorHSV(a, b, brightness)); // 灯珠亮度
}
pixels.show(); // 显示
}
lastTriggerTime = millis(); // 记录最后一次触发时间
isWaterfall = true;
} else {
if (millis() - lastTriggerTime >= 2000 && isWaterfall) {
pixels.clear(); // 关闭所有灯珠
pixels.show(); // 显示
isWaterfall = false;
} else {
pixels.clear(); // 关闭所有灯珠
pixels.show(); // 显示
}
}
delay(50); // 0.01秒
}
```
修改的主要思路是:当声音传感器的值大于30时,记录下当前时间,并将灯带设置成流水灯样式。当声音传感器的值小于等于30时,判断当前时间与上次记录的时间间隔是否超过2秒,如果超过2秒则将灯带恢复成普通灯光,否则保持关闭状态。
相关推荐
![](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)
![](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)