# how to control valve in water tank with ultrasonic sensor HC-SR04

i have a project to use ultrasonic sensor for measuring water level in water tank, in case i use ultrasonic to turn on valve , i have 3 valve in water tank . I use FIrst valve to filling water if water tank is filled with water below 10% and must be filled until 100% , and i use second valve to remove water if water tank is filled water at 100% and removed until 50% , the last i use third valve to remove water if water tank is filled water at 50% and removed until 10 % and this all process must be repeat.

So i have a problem how to make first valve active when ultrasonic detect a distance below 10 % and it always active until water tank have a containing water at 100% ( at 100% first valve don't active ) , in the second case how to make a second valve don't active if first valve doing filling a water which passed at 50 % condition

thank you bro i understand about that , but the big problem is when a tank filled the water at 10% first valve active but if the water more than 10 % first valve stopped and turn out third valve is actived. How to make first valve active when tank capacity below 10% and will always active until capacity of water at 100% (First valve non active). Because when first valve active for filling tank, water will pass process on third valve (in 50%) make a first valve will be non active and third valve will be active. I want make a firrst valve active at 10% and will be non active when capacity of tank 100%. How to make my project like that ? What must i modify in my program ?

Thanks a lot

sketch_jun02a (1).ino (1.76 KB)

/* IF:
Valve1 on : level<10
Valve1 off : level>99 (100)

Valve2 on : level>99
Valve2 off : level<50

Valve3 on : level>50
Valve3 off : level<10

This is what I understand from your post.
Using thees guidelined:
*/

``````boolean va1,va2,va3; // status for the 3 valves
void setup()
{
va1=va2=va3=false; // all closed
}

void loop()
{
// level = code that holds the current value for water level
if(level<10) {va1=true; va3=false;}
else if (level<50) {va2=false;}
else if (level>99) {va1=false; va2=true;}
else if (level>50) {va3=true;} // (exacly 50 does no change)
// set valves
digitalWrite(valve1,va1);
.....

}
``````

Thank's for coding bro, but after i was tried your code and i have found same problem. When the level at 10 % , first valve active but when the level changed from 10% to 50% first valve don't active and third valve active. So would you can help me again for this case ?

Thanks a lot

If connected OK, the 1st valve will stay unchanged until 99%

How is this powered ? Enough current ?

Are theese conditions correct?

Valve1 on : level<10
Valve1 off : level>99 (100)

Valve2 on : level>99
Valve2 off : level<50

Valve3 on : level>50
Valve3 off : level<10