If statement coding problem

Hi,

Basically I have two switches, and each switch when in the on position causes an led to blink, until the switch is turned off.

My first switch works fine, but when it is turned on, and the other switch is turned off, the led that is supposed to be off is blinking.

I think it might be an error in my coding.

I would be grateful if someone could please advise me.

Code is below, Thanks

void loop()
{
{
if (digitalRead(leftbutton) == LOW)

{
digitalWrite(indicatorleft, HIGH);
delay(200);
digitalWrite(indicatorleft,LOW);
delay(200);
}
if (digitalRead(leftbutton) ==HIGH)
{
digitalWrite(indicatorleft, LOW);
}

else

{
if (digitalRead(rightbutton) == LOW)
{
digitalWrite(indicatorright, HIGH);
delay(200);
digitalWrite(indicatorright,LOW);
delay(200);
}
if (digitalRead(rightbutton) ==HIGH)
{
digitalWrite(indicatorright, LOW);
}

}

}

lcd.setCursor(0, 1);
//print mph once a second
displayMPH();
delay(1000);
}

getting rid of the 'else'..........still have same problem

Since you are in a loop, this will blink indicatorleft whenever leftbutton is held low:

if (digitalRead(leftbutton) == LOW)
 
  {
    digitalWrite(indicatorleft, HIGH);  //Blink LED on every time through loop
    delay(200);
    digitalWrite(indicatorleft,LOW);   //Blink LED off every time through loop
    delay(200);
  }

You could use an else-statement here:

  if (digitalRead(leftbutton) ==HIGH)
  {
    digitalWrite(indicatorleft, LOW);
  }

Because if leftbutton is not low, we know it's high...

  else
  {
    digitalWrite(indicatorleft, LOW);
  }

There is one slight difference though.... The if-else statement checks the state of leftbutton before the delay. Your way, you are reading the leftbutton again after the delays
}

could you post the code.

what about switch debounce?

The way you wrote your sketch the RIGHT indicator will only blink if BOTH buttons are ON. Removing the 'else' in the middle should have fixed it. If that did not fix it you must have made other changes that broke it in a different way/

void loop() {
  {
    // If the LEFT button is ON...
    if (digitalRead(leftbutton) == LOW) {
      // ... turn the LEFT indicator on and off
      digitalWrite(indicatorleft, HIGH);
      delay(200);
      digitalWrite(indicatorleft, LOW);
      delay(200);
    }

    // If the LEFT button is OFF...
    if (digitalRead(leftbutton) == HIGH) {
      // ... turn LEFT indicator off
      digitalWrite(indicatorleft, LOW);
    } else {  // If the LEFT button is ON...
      // ... AND the RIGHT button is ON ...
      if (digitalRead(rightbutton) == LOW) {
        // ... turn the RIGHT indicator on and off
        digitalWrite(indicatorright, HIGH);
        delay(200);
        digitalWrite(indicatorright, LOW);
        delay(200);
      }

      // If the RIGHT button is OFF...
      if (digitalRead(rightbutton) == HIGH) {
        // ... turn RIGHT indicator off
        digitalWrite(indicatorright, LOW);
      }

    }
  }

  lcd.setCursor(0, 1);
  //print mph once a second
  displayMPH();
  delay(1000);
}

anilkunchalaece:
could you post the code.

what about switch debounce?

Your code has to be fast enough to catch them first. His has delays, those make other problems.

GoForSmoke:
Your code has to be fast enough to catch them first. His has delays, those make other problems.

Thanks for noticing .. I did miss those delay's