0

我有两个按钮和五个 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);
          }
    }
} 
4

3 回答 3

3

如果绘制 I/O 映射会更容易:

switchL switchR |  lR   lG   mB   rG   rR
   0       0    |  1    0    0    0    1
   0       1    |  1    0    0    1    0
   1       0    |  0    1    0    0    1
   1       1    |  0    0    1    0    0

并将输出写为输入的函数:

digitalWrite(3, !switchL            );
digitalWrite(4,  switchL && !switchR);
digitalWrite(5,  switchL &&  switchR);
digitalWrite(6, !switchL &&  switchR);
digitalWrite(7,             !switchR);

如果您更喜欢继续使用嵌套IFs,则必须始终牢记每个输出都是两个输入的函数。

于 2013-12-17T09:36:06.417 回答
0

I have a dim understanding of the functions you were using, however it was your answer that lead me to solve my issue. All I needed to was add another conditional in two of my ifs.

    if (switchL == HIGH && switchR == LOW) { //When the left button is pressed but the right button is not
  digitalWrite(4, HIGH);
  digitalWrite(3, LOW);
} else {
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH); 
}
 if (switchR == HIGH && switchL == LOW) { //When the right button is pressed but the left button is not
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);       
} else {
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH); 
}
于 2013-12-19T20:20:21.403 回答
0

你试图想得太多。首先,在您的第二次最后if测试中,您正在设置D5 HIGH然后立即测试它是否为HIGH. 不可能是其他任何东西,因此else决赛中的if永远不会被执行。

我要做的就是将您的棘手位(双按钮测试)移到代码的顶部,然后在else上述测试中测试其他每个按钮。现在阅读起来比它必须的要难。这是代码:

void loop() {
  switchL = digitalRead(2);
  switchR = digitalRead(8);

    // first test if both buttons are pressed
    if (switchL == HIGH && switchR == HIGH){
      digitalWrite(5, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
    } else {
          // now that's out the way, we test for everything else as a whole here
          // first test switchL
        if (switchL == HIGH) {
          digitalWrite(4, HIGH);
          digitalWrite(3, LOW);
        } else {
          digitalWrite(4, LOW);
          digitalWrite(3, HIGH); 
        } // end if switchL
          // then test switchR
        if (switchR == HIGH) {
          digitalWrite(6, HIGH);
          digitalWrite(7, LOW);       
        } else {
          digitalWrite(6, LOW);
          digitalWrite(7, HIGH); 
        } //end if switchR
    }     //end else of both high
} 
于 2014-02-28T11:54:08.707 回答