Programming issue

Hello everyone.

can someone help me through this.
else statement is executing even though the if statement is true.
i am usin serial montior for comunicatin with arduino and if type a command which is true for my programm its excuteing the if statemnt which is true and as well as else statement which i dont want to happen.
below is the code and picture od serialmonitor.

Thank you in Advance

// Global Variables
int RS1_IL = 3; // intializig names to Pins.
int RS2_KL30 = 4;
int RS3_KL30C = 5;
int RS4_KL31_Mio = 6;
int RS5_KL31_Extern = 7;
int interuptpin = 2;
char command; // a variable to hold the command from serial monitor.
int Relaya = 0; // Variables to follow the relay status.
int Relayb = 0;
int Relayc = 0;
int Relayd = 0;
int Relaye = 0;

void setup() {
//serialport begin
Serial.begin(9600);
Serial.println("");
// set output pins
pinMode(RS1_IL,OUTPUT);
pinMode(RS2_KL30,OUTPUT);
pinMode(RS3_KL30C,OUTPUT);
pinMode(RS4_KL31_Mio,OUTPUT);
pinMode(RS5_KL31_Extern,OUTPUT);
pinMode(interuptpin,OUTPUT);
digitalWrite(interuptpin,LOW);
attachInterrupt(0,safetytrigger,HIGH); //seting up interupt

}

void loop()
{
//Have the arduino wait to receive input
while(Serial.available() == 0);
// Read input
command = Serial.read();
if (command == '1')
{
digitalWrite(RS1_IL,HIGH);
Serial.println("RS1_IL switch is open");
Relaya = Relaya + 1;
delay(100);
}
else if (command == '0')
{
digitalWrite(RS1_IL,LOW);
Serial.println("RS1_IL switch is close");
Relaya = Relaya - 1;
delay(100);
}
else if (command == '3')
{
digitalWrite(RS2_KL30,HIGH);
Serial.println("RS2_KL30 switch is open");
Relayb = Relayb + 1;
delay(100);
}
else if (command == '2')
{
digitalWrite(RS2_KL30,LOW);
Serial.println("RS2_KL30 switch is close");
Relayb = Relayb - 1;
delay(100);
}
else if (command == '5')
{
digitalWrite(RS3_KL30C,HIGH);
Serial.println("RS3_KL30C switch is open");
Relayc = Relayc + 1;
delay(100);
}
else if (command == '4')
{
digitalWrite(RS3_KL30C,LOW);
Serial.println("RS3_KL30C switch is Close");
Relayc = Relayc - 1;
delay(100);
}
else if (command == '7')
{
digitalWrite(RS4_KL31_Mio,HIGH);
Serial.println("RS4_KL31_Mio switch is open");
Relayd = Relayd + 1;
delay(100);
}
else if (command == '6')
{
digitalWrite(RS4_KL31_Mio,LOW);
Serial.println("RS4_KL31_Mio switch is close");
Relayd = Relayd - 1;
delay(100);
}
else if (command == '9')
{
digitalWrite(RS5_KL31_Extern,HIGH);
Serial.println("RS5_KL31_Extern switch is open");
Relaye = Relaye + 1;
delay(100);
}
else if (command == '8')
{
digitalWrite(RS5_KL31_Extern,LOW);
Serial.println("RS5_KL31_Extern switch is close");
Relaye = Relaye - 1;
delay(100);
}
else if(command == 'a')
{
Serial.println("saftey Mode- all relays are reseted");
digitalWrite(2,HIGH);
delay(100);
}
else if ((Relaya>1)||(Relayb>1)|| (Relayc>1) || (Relayd>1) || (Relaye>1))
{
Serial.println("saftey mode: All relays are reseted ");
digitalWrite(2,HIGH);
delay(100);
}
else
{
Serial.println("Invalid command received"); //here is where i have problem. every time this command is executing
}
}

void safetytrigger()
{

Relaya=0;
Relayb=0;
Relayc=0;
Relayd=0;
Relaye=0;
digitalWrite(2,LOW);
digitalWrite(RS1_IL,LOW);
digitalWrite(RS2_KL30,LOW);
digitalWrite(RS3_KL30C,LOW);
digitalWrite(RS4_KL31_Mio,LOW);
digitalWrite(RS5_KL31_Extern,LOW);

}

You missed off the important bottom of your serial monitor screenshot; it contains your answer

Please remember to use code tags when posting code.

There's a very similar topic here

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using?
Also consider using switch.. case function istead of all the if.. also if...also if..

Thanks.. Tom.. :slight_smile:

@david_24

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

@david_24

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

You are treating the line ending characters as invalid commands. You can fix that by adding:

 if (command == '\n' || command == '\r')
 {
  // Ignore line ending characters
 }
else if (command == '1')
{

...or see reply #1

Note: There is a switch/case statement designed to simplify long strings of:

  if (variable == constant1)
  else if (variable == constant2)
  else if (variable == constant)

Here is how it would be used to make your command selection more readable:

 // Read input
  command = Serial.read();
  switch (command)
  {
    case '1':
      digitalWrite(RS1_IL, HIGH);
      Serial.println("RS1_IL switch is open");
      Relaya = Relaya + 1;
      delay(100);
      break;


    case '0':
      digitalWrite(RS1_IL, LOW);
      Serial.println("RS1_IL switch is close");
      Relaya = Relaya - 1;
      delay(100);
      break;


    case '3':
      digitalWrite(RS2_KL30, HIGH);
      Serial.println("RS2_KL30 switch is open");
      Relayb = Relayb + 1;
      delay(100);
      break;


    case '2':
      digitalWrite(RS2_KL30, LOW);
      Serial.println("RS2_KL30 switch is close");
      Relayb = Relayb - 1;
      delay(100);
      break;


    case '5':
      digitalWrite(RS3_KL30C, HIGH);
      Serial.println("RS3_KL30C switch is open");
      Relayc = Relayc + 1;
      delay(100);
      break;


    case '4':
      digitalWrite(RS3_KL30C, LOW);
      Serial.println("RS3_KL30C switch is Close");
      Relayc = Relayc - 1;
      delay(100);
      break;


    case '7':
      digitalWrite(RS4_KL31_Mio, HIGH);
      Serial.println("RS4_KL31_Mio switch is open");
      Relayd = Relayd + 1;
      delay(100);
      break;


    case '6':
      digitalWrite(RS4_KL31_Mio, LOW);
      Serial.println("RS4_KL31_Mio switch is close");
      Relayd = Relayd - 1;
      delay(100);
      break;


    case '9':
      digitalWrite(RS5_KL31_Extern, HIGH);
      Serial.println("RS5_KL31_Extern switch is open");
      Relaye = Relaye + 1;
      delay(100);
      break;


    case '8':
      digitalWrite(RS5_KL31_Extern, LOW);
      Serial.println("RS5_KL31_Extern switch is close");
      Relaye = Relaye - 1;
      delay(100);
      break;


    case 'a':
      Serial.println("saftey Mode- all relays are reseted");
      digitalWrite(2, HIGH);
      delay(100);
      break;


    default:
      if ((Relaya > 1) || (Relayb > 1) || (Relayc > 1) || (Relayd > 1) || (Relaye > 1))
      {
        Serial.println("saftey mode: All relays are reseted ");
        digitalWrite(2, HIGH);
        delay(100);
      }
      else
      {
        Serial.println("Invalid command received");  //here is where i have problem. every time this command is executing
      }
  }

Hello everyone.
thank you all for the reply and suggestions. i am sorry for not Autoformating the code i will remember next time.
@ johnwasser
i used ths code but the else statement was not executing i dont get it why .

if (command == '\n' || command == '\r')[color=#222222][/color]
 {[color=#222222][/color]
  // Ignore line ending characters[color=#222222][/color]
 }[color=#222222][/color]
else if (command == '1')[color=#222222][/color]
{

and then i used it has a else if statement and now it works as expected

else if (command == '\n' || command == '\r')
 {
  // Ignore line ending characters
 }
  else
  {
    Serial.println("Invalid command received");
  }
}

i will take suggestion and use case statement instead of if else statement.
Thank you once again for valueable time.

Regards
david

Or you could simply not send the carriage return/line-feed in the first place.
See reply #1