title: Arduino: wait for serial input
body: <p>I am writing a simple arduino code. I have two leds and corresponding two switches. When one switch is pressed one led is on and other is off. Then there is a Serial.read function, which reads reset from the computer. Then both switch are off. After that other switch is pressed and other led is on. My problem is when one switch is on other shouldn't be working till the Serial.read happens.But in my case, when led1 is on if I press switch2 led2 is on  and led1 is off. But that is not my desired operation. I want to make the logic that when led1 is on if I press switch2 led2 shouldn't be on and wait for the Serial.read to happen. Here is my code. I need to know what should be the correction in the logic:</p>
  
  <pre><code>int switch1 = 2;
  int motorled1 = 3;
  int switch2 = 4;
  int motorled2 = 5;
  int d1=2; 
  int d2=3; 
  int reset1 = 0;
  int reset2= 0;
  
  
  void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(2, INPUT);
    pinMode(4, INPUT);
    pinMode(3, OUTPUT);
    pinMode(5, OUTPUT);
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
    d1=digitalRead(2);
    d2=digitalRead(4);
  
    if (d1==1)
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, LOW);
  
      if (Serial.available() &gt; 0)
      {
        Serial.write(1);
      }
      if (Serial.available() &gt; 0)
      {
        reset1 = (Serial.read());
  
        digitalWrite(motorled1, LOW);
        digitalWrite(motorled2, LOW);
      }
    }
    else if (d2==1)
    {
      digitalWrite(3, LOW);
      digitalWrite(5, HIGH);
  
      if (Serial.available() &gt; 0)
      { 
        Serial.write(2);
      }
      if (Serial.available() &gt; 0)
      {
        reset2 = (Serial.read());
  
        digitalWrite(motorled1, LOW);
        digitalWrite(motorled2, LOW);
      }
    }
  }
  </code></pre>