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