使用Arduino IDE编写代码,要求:1.初始状态三色LED熄灭2.使用人体红外传感器检测有人靠近时,三色LED灯发出绿色呼吸光,3.碰撞传感器感受到信号时,三色LED灯由绿色呼吸光转为红色光,六秒后转为蓝色光。4.如果感觉到触摸传感器信号,则蓝色光慢慢转为初始绿色呼吸光10秒后熄灭,如果没有检测到触摸传感器信号则保持蓝色光一分钟后,转为初始绿色呼吸光,10秒后熄灭。完成循环。ps:LED灯用的是四阵脚版本。
时间: 2023-10-08 16:08:36 浏览: 98
以下是代码实现:
```arduino
int redPin = 9; //红色LED引脚
int greenPin = 10; //绿色LED引脚
int bluePin = 11; //蓝色LED引脚
int touchSensorPin = 2; //触摸传感器引脚
int collisionSensorPin = 3; //碰撞传感器引脚
int infraredSensorPin = 4; //人体红外传感器引脚
int breathSpeed = 20; //呼吸灯速度(越小速度越快)
int fadeSpeed = 10; //颜色渐变速度(越小速度越快)
bool isTouched = false; //是否检测到触摸传感器信号
bool isCollisionDetected = false; //是否检测到碰撞传感器信号
bool isPersonDetected = false; //是否检测到人体红外传感器信号
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(touchSensorPin, INPUT);
pinMode(collisionSensorPin, INPUT);
pinMode(infraredSensorPin, INPUT);
//初始状态三色LED熄灭
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
Serial.begin(9600); //打开串口,方便调试
}
void loop() {
//检测触摸传感器信号
if (digitalRead(touchSensorPin) == HIGH) {
isTouched = true;
} else {
isTouched = false;
}
//检测碰撞传感器信号
if (digitalRead(collisionSensorPin) == HIGH) {
isCollisionDetected = true;
} else {
isCollisionDetected = false;
}
//检测人体红外传感器信号
if (digitalRead(infraredSensorPin) == HIGH) {
isPersonDetected = true;
} else {
isPersonDetected = false;
}
//根据传感器状态控制LED灯颜色
if (isCollisionDetected) {
//碰撞传感器感受到信号时,三色LED灯由绿色呼吸光转为红色光,六秒后转为蓝色光。
setColor(255, 0, 0); //红色
delay(6000);
setColor(0, 0, 255); //蓝色
delay(1000);
} else if (isTouched) {
//如果感觉到触摸传感器信号,则蓝色光慢慢转为初始绿色呼吸光10秒后熄灭
fadeToColor(0, 0, 255, 0, 255, 0, fadeSpeed); //蓝色渐变到绿色
delay(10000);
setColor(0, 0, 0); //熄灭
} else if (isPersonDetected) {
//使用人体红外传感器检测有人靠近时,三色LED灯发出绿色呼吸光
breathe(0, 255, 0, breathSpeed); //绿色呼吸灯
} else {
//保持蓝色光一分钟后,转为初始绿色呼吸光,10秒后熄灭
setColor(0, 0, 255); //蓝色
delay(60000);
breathe(0, 255, 0, breathSpeed); //绿色呼吸灯
delay(10000);
setColor(0, 0, 0); //熄灭
}
}
//设置LED灯颜色
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
//呼吸灯效果
void breathe(int red, int green, int blue, int speed) {
for (int i = 0; i <= 255; i += speed) {
analogWrite(redPin, map(i, 0, 255, 0, red));
analogWrite(greenPin, map(i, 0, 255, 0, green));
analogWrite(bluePin, map(i, 0, 255, 0, blue));
delay(10);
}
for (int i = 255; i >= 0; i -= speed) {
analogWrite(redPin, map(i, 0, 255, 0, red));
analogWrite(greenPin, map(i, 0, 255, 0, green));
analogWrite(bluePin, map(i, 0, 255, 0, blue));
delay(10);
}
}
//颜色渐变效果
void fadeToColor(int startRed, int startGreen, int startBlue, int endRed, int endGreen, int endBlue, int speed) {
float rStep = (endRed - startRed) / 255.0;
float gStep = (endGreen - startGreen) / 255.0;
float bStep = (endBlue - startBlue) / 255.0;
for (int i = 0; i <= 255; i += speed) {
analogWrite(redPin, startRed + i * rStep);
analogWrite(greenPin, startGreen + i * gStep);
analogWrite(bluePin, startBlue + i * bStep);
delay(10);
}
}
```
需要注意的细节:
1.使用 PWM 脉宽调制来控制 LED 亮度,要将 LED 引脚设置为输出模式。
2.触摸传感器、碰撞传感器、人体红外传感器的输出信号是数字信号,可以通过 digitalRead() 函数读取。
3.为了方便调试,可以使用 Serial.println() 函数将一些状态信息输出到串口,使用串口助手进行查看。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)