基于ATMEGA328P用3个开关控制3个灯泡,灯泡只需要亮3秒,然后就关闭,三个开关同时按下去灯泡可以同时亮,在每一个灯泡加一个提示灯,不按下开关时候提示灯是亮的,按下去后提示灯是不亮的
时间: 2023-05-23 18:01:24 浏览: 134
可以使用以下代码来实现:
```
const int switch1Pin = 2;
const int switch2Pin = 3;
const int switch3Pin = 4;
const int led1Pin = 5;
const int led2Pin = 6;
const int led3Pin = 7;
const int indicator1Pin = 8;
const int indicator2Pin = 9;
const int indicator3Pin = 10;
const int duration = 3000; // 每个灯泡亮3秒钟
void setup() {
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);
pinMode(switch3Pin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(indicator1Pin, OUTPUT);
pinMode(indicator2Pin, OUTPUT);
pinMode(indicator3Pin, OUTPUT);
// 所有提示灯都是亮的
digitalWrite(indicator1Pin, HIGH);
digitalWrite(indicator2Pin, HIGH);
digitalWrite(indicator3Pin, HIGH);
}
void loop() {
if (digitalRead(switch1Pin) == LOW &&
digitalRead(switch2Pin) == LOW &&
digitalRead(switch3Pin) == LOW) {
// 所有开关都按下去了,三个灯泡都要同时亮
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
// 关闭所有提示灯
digitalWrite(indicator1Pin, LOW);
digitalWrite(indicator2Pin, LOW);
digitalWrite(indicator3Pin, LOW);
// 等待3秒钟
delay(duration);
// 关闭所有灯
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
// 打开所有提示灯
digitalWrite(indicator1Pin, HIGH);
digitalWrite(indicator2Pin, HIGH);
digitalWrite(indicator3Pin, HIGH);
}
else {
if (digitalRead(switch1Pin) == LOW) {
digitalWrite(led1Pin, HIGH); // 开启灯泡1
digitalWrite(indicator1Pin, LOW); // 关闭提示灯1
delay(duration); // 等待3秒钟
digitalWrite(led1Pin, LOW); // 关闭灯泡1
digitalWrite(indicator1Pin, HIGH); // 打开提示灯1
}
if (digitalRead(switch2Pin) == LOW) {
digitalWrite(led2Pin, HIGH); // 开启灯泡2
digitalWrite(indicator2Pin, LOW); // 关闭提示灯2
delay(duration); // 等待3秒钟
digitalWrite(led2Pin, LOW); // 关闭灯泡2
digitalWrite(indicator2Pin, HIGH); // 打开提示灯2
}
if (digitalRead(switch3Pin) == LOW) {
digitalWrite(led3Pin, HIGH); // 开启灯泡3
digitalWrite(indicator3Pin, LOW); // 关闭提示灯3
delay(duration); // 等待3秒钟
digitalWrite(led3Pin, LOW); // 关闭灯泡3
digitalWrite(indicator3Pin, HIGH); // 打开提示灯3
}
}
}
```
阅读全文