Problem setting upper and lower limits in water level control system.

Hey arduino-guru guys!

I have a simple eTape measuring the water level on my system and want to turn on a pump when the water level reaches a certain threshold.

I tried defining an if <= argument, but the relay controlling the pump goes crazy due to the variation of the sensor signal.

What I want to do now is defining an upper and lower value, as following the code.

//***water level setup***//
// Configuration values:
#define SERIES_RESISTOR     560    // Value of the series resistor in ohms.    
#define SENSOR_PIN          0      // Analog pin which is connected to the sensor. 

//***relay setup***//
// Relay pin is controlled with D8. The active wire is connected to Normally Closed and common
int relay = 8;
volatile byte relayState = HIGH;

void setup() {
  // Pin for relay module set as output
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
  Serial.begin(115200);
 }

void loop() {
  //water level sensor
  // Measure sensor resistance.
  float resistance = readResistance(SENSOR_PIN, SERIES_RESISTOR);
  Serial.print("Resistance: ");
  Serial.print(resistance, 2);
  Serial.println(" ohms");

 [color=red] //relay
  if (resistance = 1200) {
    digitalWrite(relay, LOW);
    Serial.println("Pump ON");
  }
  else {
    if (resistance = 1000){
    digitalWrite(relay, HIGH);
    Serial.println("Pump OFF"); 
    }
  }
  // Delay for a 0.5 seconds.
  delay(500);
}


float readResistance(int pin, int seriesResistance) {
  // Get ADC value.
  float resistance = analogRead(pin);
  // Convert ADC reading to resistance.
  resistance = (1023.0 / resistance) - 1.0;
  resistance = seriesResistance / resistance;
  return resistance;
}

But when I use this code the pump is always on, even if the lower limit is reached.

Is there a better way to set this range of values for my water level sensor?

Thanks!

    if (resistance = 1000)

That assigns a value of 1000 to resistance. It does not compare resistance to 1000

UKHeliBob:     if (resistance = 1000)

That assigns a value of 1000 to resistance. It does not compare resistance to 1000

hey, thanks for pointing this out! Completely missed the second =

Would be correct if I use the following synthax:

 //relay
  if (resistance == 1200) {
    digitalWrite(relay, LOW);
    Serial.println("Pump ON");
  }
  else {
    if (resistance == 1000){
    digitalWrite(relay, HIGH);
    Serial.println("Pump OFF"); 
    }
  }
  // Delay for a 0.5 seconds.
  delay(500);
}
[code]

thanks!

Unless you are absolutely certain that one or other condition will be true then it is better not to test for equality but rather to test for greater than and less than

UKHeliBob: Unless you are absolutely certain that one or other condition will be true then it is better not to test for equality but rather to test for greater than and less than

absolutely. I was testing a few parameters and faced this exact problem, the condition wasn't matched at all.

My works around was:

 //relay
  if (resistance == constrain(resistance, 1100, 1200)){
    digitalWrite(relay, LOW);
    Serial.println("Pump ON");
  }
  else {
    if (resistance == constrain(resistance, 900, 1000)){
    digitalWrite(relay, HIGH);
    Serial.println("Pump OFF"); 
    }
  }
  // Delay for a 0.5 seconds.
  delay(500);
}

Now the project is running smoothly! Thanks, UKHeliBob

Try the following:-

 if (resistance >= 1200) {   // If the resistance value is greater than or equals 1200 turn the pump on
    digitalWrite(relay, LOW);
    Serial.println("Pump ON");
  }


  if (resistance <= 1000){  // If the resistance value is less than or equals 1000 turn the pump off
    digitalWrite(relay, HIGH);
    Serial.println("Pump OFF");
  }

  // Otherwise leave the pump in it's current state

  // Delay for a 0.5 seconds.
  delay(500);
}

If you only use equivalents then it is possible your 1200 and 1000 levels could be exceed during your 500ms delay. This way if the resistance falls outside the range 1000 to 1200 the pump state will be set to correct the situation.

If your resistance measurement fluctuates try measuring it a number of times and taking the average.

Ian