Some "else if" problems

Hello,

I'm trying to solve a problem with the attached code. It should read some sensor data, check if the readings are within some set values, and based on that, turn on and off some fans (via digitalWrite). Also at the same time, the readings are sent via serial to a windows app.

However when a sensor temperature is reaching the first set value (45 degrees) it seems that the entire part of if-else-if portions of the code and digitalwrite commands are being skipped.
The read values are sent to the serial port without any problems however.

There seem to be some problems with this part of the code:

if ((newTempVal == -127) or (newTempVal1 == -127) or (newTempVal2 == -127) or (newTempVal3 == -127))
{
}

else if (newTempVal2 > 45) //temp miez smps de la care pleaca ventilator mic
{ digitalWrite(1, HIGH);
  Serial.print("<FH>");
  digitalWrite(7, HIGH);
  Serial.print("<XA>");  
}

else if ((newTempVal2 > 50) and (newTempVal3 < 50) and (newTempVal < 50)) // temp miez smps la care pleaca ventilator mare dreapta
{
  digitalWrite(4, HIGH);
  Serial.print("<SH>");
  digitalWrite(3, LOW);
  Serial.print("<AL>");
  digitalWrite(1, HIGH);
  Serial.print("<FH>");
  digitalWrite(7, HIGH);
  Serial.print("<XA>");  
}
else if ((newTempVal > 55) or (newTempVal3 > 55) or (newTempVal1 > 55) or (newTempVal2 > 55)) // temp a oricarui senzor de la care pleaca ventilatia full

{
  digitalWrite(4, HIGH);
  Serial.print("<SH>");
  digitalWrite(3, HIGH);
  Serial.print("<AH>");
  digitalWrite(1, HIGH);
  Serial.print("<FH>");
  digitalWrite(7, HIGH);
  Serial.print("<XA>");  
}
else if ((newTempVal2 < 40) and (newTempVal < 40) and (newTempVal1 < 40) and (newTempVal3 < 40))
{ 
  digitalWrite(4, LOW);
  Serial.print("<SL>");
  digitalWrite(3, LOW);
  Serial.print("<AL>"); 
  digitalWrite(1, LOW);
  Serial.print("<FL>"); 
  digitalWrite(7, HIGH); 
  Serial.print("<XA>");      
 }
    
else if ((newTempVal > 70) and (newTempVal != -127))
 
 {
  digitalWrite(7, LOW);
  Serial.print("<XX>"); 
  digitalWrite(4, HIGH);
  Serial.print("<SH>");
  digitalWrite(3, HIGH);
  Serial.print("<AH>");
  digitalWrite(1, HIGH);
  Serial.print("<FH>");
  delay(300000);
 }
 
 else if ((newTempVal2 > 70) and (newTempVal2 != -127))
 {
  digitalWrite(7, LOW);
  Serial.print("<XX>"); 
  digitalWrite(4, HIGH);
  Serial.print("<SH>");
  digitalWrite(3, HIGH);
  Serial.print("<AH>");
  digitalWrite(1, HIGH);
  Serial.print("<FH>");
  delay(300000);
 }
 else if ((newTempVal3 > 70) and (newTempVal3 != -127))
 { 
  digitalWrite(7, LOW);
  Serial.print("<XX>"); 
  digitalWrite(4, HIGH);
  Serial.print("<SH>");
  digitalWrite(3, HIGH);
  Serial.print("<AH>");
  digitalWrite(1, HIGH);
  Serial.print("<FH>");
  delay(300000);
 }        
  else if ((newTempVal1 > 70) and (newTempVal1 != -127))
 { 
  digitalWrite(7, LOW);
  Serial.print("<XX>"); 
  digitalWrite(4, HIGH);
  Serial.print("<SH>");
  digitalWrite(3, HIGH);
  Serial.print("<AH>");
  digitalWrite(1, HIGH);
  Serial.print("<FH>");
  delay(300000);
 }

...yet I can't find why when newTempVal2 reaches 45 degrees, the rest of the code doesn't seem to run.
Can anyone please help?

test1.ino (6.06 KB)

else if (newTempVal2 > 45)
If this is true, none of the remaining code will run by design as you are using ‘else if’.

Ups.. that seems to be it. Since all situations must be checked, I've rearranged the else if statements from high value readings (the most unlikely scenario) to low.
Now everything is ok.

Thanks for helping a total noob, it's my first attempt at this.