# If statement coding problem

**URL:** <https://forum.arduino.cc/t/if-statement-coding-problem/304783>\
**Category:** General Guidance\
**Created:** [April 14, 2015, 4:30pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783 "2015-04-14T16:30:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 14, 2015, 4:30pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/1 "2015-04-14T16:30:17Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 14, 2015, 4:41pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/3 "2015-04-14T16:41:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DVDdoug](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dvddoug/32/671533_2.png) [@DVDdoug](https://forum.arduino.cc/u/DVDdoug)\
**Post date:** [April 14, 2015, 4:52pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/4 "2015-04-14T16:52:33Z")

</div>

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

```cpp
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:

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

```

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

```cpp
  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  
}

---

<div class="post-metadata">

**Author:** ![anilkunchalaece](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anilkunchalaece](https://forum.arduino.cc/u/anilkunchalaece)\
**Post date:** [April 14, 2015, 4:56pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/5 "2015-04-14T16:56:05Z")

</div>

could you post the code.

what about switch debounce?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [April 14, 2015, 7:21pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/6 "2015-04-14T19:21:20Z")

</div>

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/

```cpp
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);
}

```

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [April 15, 2015, 5:25am UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/7 "2015-04-15T05:25:47Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![anilkunchalaece](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anilkunchalaece](https://forum.arduino.cc/u/anilkunchalaece)\
**Post date:** [April 15, 2015, 5:37am UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/8 "2015-04-15T05:37:24Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 5:28pm UTC](https://forum.arduino.cc/t/if-statement-coding-problem/304783/9 "2021-05-05T17:28:19Z")

</div>


