我有两个按钮和五个 LED 连接。在电路中,它的顺序是按钮、五个 LED 和按钮。LED 的顺序从左到右(从第一个按钮之后开始)红色、绿色、蓝色、绿色和红色。我使用蓝色(中间)LED 来区分左右两侧。按下左按钮时,其对应的绿色按钮亮起,松开按钮时,红色按钮亮起。右侧也有相同的功能。所以我想要做的是当两个按钮都被按下时,绿灯不亮,蓝灯亮。但是,当按下两个按钮时,绿灯和蓝光都会亮起。电路问题的编程错误?这是我的代码:
//Using Arduino UNO
int switchL = 0; //Left button
int switchR = 0; //Right button
void setup() { //LED from left to right
pinMode(3, OUTPUT); //Red
pinMode(4, OUTPUT); //Green
pinMode(5, OUTPUT); //Blue
pinMode(6, OUTPUT); //Green
pinMode(7, OUTPUT); //Red
Serial.begin(9600);
}
void loop() {
switchL = digitalRead(2);
switchR = digitalRead(8);
if (switchL == HIGH) {
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
} else {
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
}
if (switchR == HIGH) {
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
} else {
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}
if (switchL == HIGH && switchR == HIGH){
digitalWrite(5, HIGH);
if (digitalRead(5) == HIGH) {
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
} else {
digitalWrite(5, LOW);
}
}
}