arduino IF statement doesn't work properly

Hi I am writing simple application so I can control devices around my house.

I have wrote the code but IF statement doesn't work with multiple conditions inside it.

Here is my code:

int red=2;
int green=3;

int status=0;

boolean status2=false;

boolean status6=false;
boolean status5=false;
boolean status4=false;
boolean status3=false;

void setup(){
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);

}

void loop(){

  if(Serial.available()>0){
    status=Serial.read();
    
      if(Serial.available()>0){
    status=Serial.read();
    if((status=='1') && (status2==false)){
      status2=true;
      digitalWrite(red,HIGH);
      Serial.println(status);
      status=0;
    }else{
      status2=false;
      digitalWrite(red,LOW);
      status=0;}

    if((status=='2') && (status3==false)){
      digitalWrite(green,HIGH);
      Serial.println(status);
      status3=true;
      status=0;
    }else{
      status3=false;
      digitalWrite(green,LOW);
      status=0;  }

      //test signal
    if(status=='e'){
      Serial.println(status);
      status=0;
    }
  }
}
}

The problem is when I send the data, arduino stops on the first condition and will not continue come-down to next condition. I have tested this with Java and console and works fine. Maybe arduino doesn't support multiple if conditions?

Please advice me.

Regards,
Lazar

If 'status' is not '1', you set it to 0.

Therefore this will always fail:-

if ((status == '2') && (status3 == false))

Also, at the beginning of 'loop()', you do a 'Serial.read()', then discard the returned value and do it again. Was this intentional?

Edit: You're also declaring 'status' as an int. Try 'char', or alternatively, declare it as 'int' but caste to 'char' for the serial prints:-

Serial.println((char)status);

See if this comes closer to doing what you want:-

int crvena = 2;
int zelena = 3;

//int status = 0;
char status = 0;
boolean status2 = false;
boolean status6 = false;
boolean status5 = false;
boolean status4 = false;
boolean status3 = false;

void setup()
{
    Serial.begin(9600);
    pinMode(crvena, OUTPUT);
    pinMode(zelena, OUTPUT);

}

void loop()
{
    //    if (Serial.available() > 0)
    //    {
    //    status = Serial.read();

    if (Serial.available() > 0)
    {
        status = Serial.read();
        if ((status == '1') && (status2 == false))
        {
            status2 = true;
            digitalWrite(crvena, HIGH);
            Serial.println(status);
            status = 0;
        }
        else
        {
            status2 = false;
            digitalWrite(crvena, LOW);
//            status = 0;
        }

        if ((status == '2') && (status3 == false))
        {
            digitalWrite(zelena, HIGH);
            Serial.println(status);
            status3 = true;
            status = 0;
        }
        else
        {
            status3 = false;
            digitalWrite(zelena, LOW);
//            status = 0;
        }

        //test signal
        if (status == 'e')
        {
            //digitalWrite(zelena,LOW);
            Serial.println(status);
            status = 0;
        }
    }
    //    }
}

And status should be defined as a char

J-M-L:
And status should be defined as a char

We both said the same thing at the same time - I was editing. :smiley:

:slight_smile:

Arduino does what you tell it to do, not necessarily what you want it to do.