基于ATMEGA328P用3个开关控制3个灯泡,灯泡只需要亮3秒,然后就关闭,三个开关同时按下去灯泡可以同时亮,在每一个灯泡加一个提示灯,不按下开关时候提示灯是亮的,按下去后提示灯是不亮的
时间: 2023-05-23 20:01:26 浏览: 74
您可以使用以下代码实现:
//定义引脚
int led1 = 2;
int led2 = 3;
int led3 = 4;
int switch1 = 5;
int switch2 = 6;
int switch3 = 7;
int lightIndicator1 = 8;
int lightIndicator2 = 9;
int lightIndicator3 = 10;
void setup() {
//定义引脚模式
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
pinMode(switch3, INPUT_PULLUP);
pinMode(lightIndicator1, OUTPUT);
pinMode(lightIndicator2, OUTPUT);
pinMode(lightIndicator3, OUTPUT);
digitalWrite(lightIndicator1, HIGH);
digitalWrite(lightIndicator2, HIGH);
digitalWrite(lightIndicator3, HIGH);
}
void loop() {
//判断三个开关是否同时按下
if (digitalRead(switch1) == LOW && digitalRead(switch2) == LOW && digitalRead(switch3) == LOW) {
//同时控制三个灯亮并同步点亮提示灯
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(lightIndicator1, LOW);
digitalWrite(lightIndicator2, LOW);
digitalWrite(lightIndicator3, LOW);
delay(3000); //灯亮3秒
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(lightIndicator1, HIGH);
digitalWrite(lightIndicator2, HIGH);
digitalWrite(lightIndicator3, HIGH);
} else {
//单独控制每个灯亮并提示灯亮
if (digitalRead(switch1) == LOW) {
digitalWrite(led1, HIGH);
digitalWrite(lightIndicator1, LOW);
delay(3000); //灯亮3秒
digitalWrite(led1, LOW);
digitalWrite(lightIndicator1, HIGH);
}
if (digitalRead(switch2) == LOW) {
digitalWrite(led2, HIGH);
digitalWrite(lightIndicator2, LOW);
delay(3000); //灯亮3秒
digitalWrite(led2, LOW);
digitalWrite(lightIndicator2, HIGH);
}
if (digitalRead(switch3) == LOW) {
digitalWrite(led3, HIGH);
digitalWrite(lightIndicator3, LOW);
delay(3000); //灯亮3秒
digitalWrite(led3, LOW);
digitalWrite(lightIndicator3, HIGH);
}
}
}
阅读全文