mardi 16 mars 2021

If statements not working as expected on nodemcu

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.

Aucun commentaire:

Enregistrer un commentaire