2
votes

I am having a bit of puzzling situation here with my experiment. I am simply measuring the soil moisture using a capacitive sensor and reading via analog pin of nodemcu. Afterwards, I am categorizing soil condition as wet, mid-level or dry. I want to use simple if-else statements for this. But, for some reason they arent working as intended. Only first if is executed, the condition is satisfied when it shouldnt...

  const int SOIL_MOISTURE_DATA_NOT_FOUND = 0;
  const int SOIL_TOO_WET = 1;
  const int SOIL_MID_LEVEL_MOISTURE = 2;
  const int SOIL_TOO_DRY = 3;

  

When I use else if statements, it executes only first statement and returns..

  int soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;

  int rawHumiditySensorValue = analogRead(A0);

  Serial.print("Moisture value : ");
  Serial.println(rawHumiditySensorValue);
  
  if( 500 < rawHumiditySensorValue <= 750 )
  {
    Serial.println("SOIL_TOO_WET");
    soilStatus = SOIL_TOO_WET;
  }
  else if( 750 < rawHumiditySensorValue <= 820 )
  {
    soilStatus = SOIL_MID_LEVEL_MOISTURE;
    Serial.println("SOIL_MID_LEVEL_MOISTURE");
  }
  else if( 820 < rawHumiditySensorValue <= 920 )
  {
    soilStatus = SOIL_TOO_DRY;
  }
   // Invalid reading / no reading cases:
  else if( rawHumiditySensorValue < 500 || rawHumiditySensorValue > 1000 )
  {
    soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
  }
    
  Serial.print("Soil Status :");
  Serial.println(soilStatus);

Output from serial monitor:

enter image description here

I removed if else, all the if statements are getting executed regardless of the variable value

  int soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;

  int rawHumiditySensorValue = analogRead(A0); //readAnalogDataFromHydroSensor(pinID);

  Serial.print("Moisture value : ");
  Serial.println(rawHumiditySensorValue);
  
  if( 500 < rawHumiditySensorValue <= 750 )
  {
    Serial.println("SOIL_TOO_WET");
    soilStatus = SOIL_TOO_WET;
  }
  if( 750 < rawHumiditySensorValue <= 820 )
  {
    soilStatus = SOIL_MID_LEVEL_MOISTURE;
    Serial.println("SOIL_MID_LEVEL_MOISTURE");
  }
  if( 820 < rawHumiditySensorValue <= 920 )
  {
    soilStatus = SOIL_TOO_DRY;
  }
  // Invalid reading / no reading cases:
  if( rawHumiditySensorValue < 500 || rawHumiditySensorValue > 1000 )
  {
    soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
  }
    
  Serial.print("Soil Status :");
  Serial.println(soilStatus);

I get following output on serial monitor:

enter image description here

What am I missing here? I checked the datatype of all local vars, all are set to int.

1
500 < rawHumiditySensorValue <= 750 should be 500 < rawHumiditySensorValue && rawHumiditySensorValue <= 750. etc.Ian Abbott

1 Answers

2
votes

The relational operators are binary operators with left-to-right associativity. The result is 0 or 1.

The expression 500 < rawHumiditySensorValue <= 750 is equivalent to (500 < rawHumiditySensorValue) <= 750. Since the result of (500 < rawHumiditySensorValue) will be 0 or 1, and since both those values are less than or equal to 750, the result of the overall expression is 1.

What you probably wanted is 500 < rawHumiditySensorValue && rawHumiditySensorValue <= 750.


You could simplify the use of relational operators in your code using else if:

  // Invalid reading / no reading cases:
  if( rawHumiditySensorValue < 500 || rawHumiditySensorValue > 1000 )
  {
    soilStatus = SOIL_MOISTURE_DATA_NOT_FOUND;
  }
  else if( rawHumiditySensorValue <= 750 )
  {
    soilStatus = SOIL_TOO_WET;
  }
  else if( rawHumiditySensorValue <= 820 )
  {
    soilStatus = SOIL_MID_LEVEL_MOISTURE;
  }
  else if( rawHumiditySensorValue <= 920 )
  {
    soilStatus = SOIL_TOO_DRY;
  }
  else
  {
    soilStatus = SOIL_FAR_TOO_DRY; /* Original code did not handle this case! */
  }