使用Arduino IDE编写代码,要求:1.使用人体红外传感器检测有人靠近时,三色LED灯发出绿色呼吸光,2.碰撞传感器感受到信号时,三色LED灯由绿色呼吸光转为红色光,六秒后转为蓝色光。3.如果感觉到触摸传感器信号,则蓝色光慢慢转为初始绿色呼吸光,如果没有检测到触摸传感器信号则保持蓝色光一分钟后,转为初始绿色呼吸光。完成循环。
时间: 2023-10-05 09:13:44 浏览: 65
以下是Arduino IDE的代码实现:
```
int pirSensorPin = 2; // 人体红外传感器接口
int touchSensorPin = 3; // 触摸传感器接口
int collisionSensorPin = 4; // 碰撞传感器接口
int redPin = 5; // 红色LED接口
int greenPin = 6; // 绿色LED接口
int bluePin = 7; // 蓝色LED接口
int blueBrightness = 0; // 蓝色LED亮度
int blueFadeAmount = 5; // 蓝色LED渐变速度
int redBright = 255; // 红色LED亮度
int greenBright = 0; // 绿色LED亮度
bool touchSensorDetected = false; // 触摸传感器是否检测到信号
void setup() {
pinMode(pirSensorPin, INPUT); // 人体红外传感器输入模式
pinMode(touchSensorPin, INPUT); // 触摸传感器输入模式
pinMode(collisionSensorPin, INPUT); // 碰撞传感器输入模式
pinMode(redPin, OUTPUT); // 红色LED输出模式
pinMode(greenPin, OUTPUT); // 绿色LED输出模式
pinMode(bluePin, OUTPUT); // 蓝色LED输出模式
}
void loop() {
// 人体红外传感器检测到信号时,三色LED灯发出绿色呼吸光
if (digitalRead(pirSensorPin) == HIGH) {
// 绿色呼吸光
for (int i = 0; i < 255; i++) {
greenBright = i;
analogWrite(greenPin, greenBright);
delay(10);
}
for (int i = 255; i >= 0; i--) {
greenBright = i;
analogWrite(greenPin, greenBright);
delay(10);
}
}
// 碰撞传感器感受到信号时,三色LED灯由绿色呼吸光转为红色光,六秒后转为蓝色光
if (digitalRead(collisionSensorPin) == HIGH) {
// 红色光
analogWrite(redPin, redBright);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(6000); // 等待6秒
// 蓝色光
while (blueBrightness < 255) {
blueBrightness += blueFadeAmount;
analogWrite(bluePin, blueBrightness);
delay(30);
}
while (blueBrightness > 0) {
blueBrightness -= blueFadeAmount;
analogWrite(bluePin, blueBrightness);
delay(30);
}
}
// 如果感觉到触摸传感器信号,则蓝色光慢慢转为初始绿色呼吸光
if (digitalRead(touchSensorPin) == HIGH) {
touchSensorDetected = true;
}
if (touchSensorDetected) {
// 绿色呼吸光
for (int i = 0; i < 255; i++) {
greenBright = i;
analogWrite(greenPin, greenBright);
delay(10);
}
for (int i = 255; i >= 0; i--) {
greenBright = i;
analogWrite(greenPin, greenBright);
delay(10);
}
touchSensorDetected = false;
} else {
// 蓝色光
while (blueBrightness < 255) {
blueBrightness += blueFadeAmount;
analogWrite(bluePin, blueBrightness);
delay(30);
}
delay(60000); // 等待60秒
while (blueBrightness > 0) {
blueBrightness -= blueFadeAmount;
analogWrite(bluePin, blueBrightness);
delay(30);
}
}
}
```
阅读全文