基于ATMEGA328P用六个开关控制六个点灯,关灯3秒后关闭
时间: 2023-05-21 15:01:01 浏览: 130
以下是基于ATMEGA328P用六个开关控制六个点灯,关灯3秒后关闭的代码:
```
int switch1 = 2;
int switch2 = 3;
int switch3 = 4;
int switch4 = 5;
int switch5 = 6;
int switch6 = 7;
int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;
int led5 = 12;
int led6 = 13;
void setup() {
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
pinMode(switch5, INPUT);
pinMode(switch6, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
}
void loop() {
if (digitalRead(switch1) == HIGH) {
digitalWrite(led1, HIGH);
} else {
digitalWrite(led1, LOW);
}
if (digitalRead(switch2) == HIGH) {
digitalWrite(led2, HIGH);
} else {
digitalWrite(led2, LOW);
}
if (digitalRead(switch3) == HIGH) {
digitalWrite(led3, HIGH);
} else {
digitalWrite(led3, LOW);
}
if (digitalRead(switch4) == HIGH) {
digitalWrite(led4, HIGH);
} else {
digitalWrite(led4, LOW);
}
if (digitalRead(switch5) == HIGH) {
digitalWrite(led5, HIGH);
} else {
digitalWrite(led5, LOW);
}
if (digitalRead(switch6) == HIGH) {
digitalWrite(led6, HIGH);
} else {
digitalWrite(led6, LOW);
}
delay(3000);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
```
希望这个代码能够帮到你!
阅读全文