You're overcomplicating this: your logic reduces to:
If water level is below LLWL, start pump
If water level is above HLWL, stop pump
Now, I would add checks that when the pump is on, you should see the transition of LLWL from on to off (make sure that pump is running and water is flowing) and also timeout if HLWL is not active in time (safety behavior in case HLWL sensor fails).
Anyway:
#define LL 2
#define HL 3
#define PUMP 4
#define ON HIGH
#define OFF LOW
pinMode(LL, INPUT_PULLUP);
pinMode(HL, INPUT_PULLUP);
pinMode(PUMP, OUTPUT);
digitalWrite(PUMP, OFF);
while(1)
{
delay(100);
// Assume sensors active LOW
if (digitalRead(LL) == LOW)
{
digitalWrite(PUMP, ON);
}
else if (digitalRead(HL == LOW)
{
digitalWrite(PUMP, OFF);
}
}
Have fun. If you need more detail than this, PM me. I have literally spent years doing motion control and fluid management & flow controllers.
It's So easy, Same as you I was making this too complicated but one of my friend corrected me.
Here is the code (Active Low Logic):
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(A0, OUTPUT); // Motor Digital Pin
}
void loop() {
// put your main code here, to run repeatedly:
int L = digitalRead(2); // Low Level Water Sensor (LLWS)
int H = digitalRead(3); // High Level Water Sensor (HLWS)
if(L == 0 && H == 0)
{
digitalWrite(A0, LOW);
}
if(L == 1 && H == 1)
{
digitalWrite(A0, HIGH);
}
delay(100);
}
From experience you will also want to add an hysteresis type of algorithm of else as you add liquid in the tank it will create turbulence that will make the level detecter go into a series of true/false/true states.
IndianaTux:
From experience you will also want to add an hysteresis type of algorithm of else as you add liquid in the tank it will create turbulence that will make the level detecter go into a series of true/false/true states.
This is exactly the system I use in my irrigation system and I had to add code to fix that exact problem.
One other thing the OP has not told us: is the water supply rate greater than the rate you are taking water out?