Relay and ultrasonic water tank level

How to make relay work only when water level 1% to fully 100%.

When the water decreases in the tank from 100% to 1% the fill pump has not turned on, but when the water reaches 1% the fill pump must be on until the water reaches the tank 100%

can someone give me a sample code of the command ?

If level <= 1 turn pump on.

If level >=100 turn pump off.

my goal is exactly what you mean, strangely in this case when the water level rises from 1% the fill pump turns on and off when it reaches 100%, and comes back on when the water level drops from 100% to 1%

I mean how about using map() function, can you explain?

Consider posting your code (in code tags, etc.)

maybe you can help?
I want when the water level reaches 1% the water pump turns on for 5 seconds to check the water rate in the inlet pipe, if there is a water rate then the pump turns on, if there is no water rate then the pump turns on for 5 seconds then turns off 5 seconds until there is a water rate in the inlet pipe .

void pumpInDelay() {
  unsigned long timeNow = millis();

  if (TankLevelPercentage <= 10) {

    if (timeNow - prevTimePumpInOn > PumpInOnDelay )  {

      prevTimePumpInOn += PumpInOnDelay;
      if ((PumpInState == HIGH) && (FlowPMIn < 2)) {


        PumpInState = LOW;
        lcd.setCursor(0, 3);
        lcd.print("       ");
      }
      else {
        PumpInState = HIGH;
        lcd.setCursor(0, 3);
        lcd.print("> Refil");
      }
      digitalWrite(PumpIn, PumpInState);

    }
    else if ((TankLevelPercentage <= 10) && (FlowPMIn > 2)) {
      PumpInState = HIGH;
    }
    else if (TankLevelPercentage >= 100 ) {
      PumpInState = LOW;
    }
  }
}```

That's not everything, but let's not go back and forth.
Consider using a "flag" (or "flag bit"), a boolean.
You can't have the supply line On when flag is asserted (a condition).
if flag is asserted and level == 1% then flag = !flag, turn on the supply.

can you modify my code? sorry I'm really confused

initial status
boolean tank_full = true;
pump is Off (disabled)

level monitor function:
if level <= 1% then tank_full = false; pump is On (enabled)
if level == 100% then tank_full = true; pump is Off (disabled)

The problem (I think) is that there is at least one other requirement such that there is also a check to verify that water is flowing from the inlet pump.

It looks to me as though a rewrite as a state machine would make things easier, but I think the OP may struggle with the refactoring.

1 Like

pseudo-code --

void setup() 
{ 
  pump_off;
  if (level < 99%)
  {
    pump_on:
    do
    {
      check_level;
    } while (level < 100%);
    pump_off;
  }
}

void loop() 
{
  do
  {
    check_level;
  } while (level >= 1%)

  pump_on;

  do
  {
    check_level;
  } while (level < 100%)

  pump_off;
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.