Controlling water pump, if statement

Im doing a project my code is verified, but I am wondering if the coding is correct based on how I have my if statements setup.

I have 2 if statements back to back but I dont see another way I could do it. I need the ultra sonic sensor to sense the water level if the tank is full thats above 0" and less that or 25" the pump should be off, I also dont want as soon as its above 25" like say 26" or more the pump should not come back on until it reaches a good distant down in the tank, say above 50".

I also installed UV lamps in the system to kill bacteria and they should not be on if water isnt passing through them, So I also wanted when the pumps came on the Uv lamps would come on just the same.

Can someone assist me with this coding.

if(distanceInch1 > 0 && distanceInch1 <= 25){
digitalWrite(uvlamp1, LOW);
digitalWrite(sourcepump1, LOW);}

if(distanceInch1  > 50) {
  digitalWrite(sourcepump1, HIGH);
  digitalWrite(uvlamp1, HIGH); }
  else
  {digitalWrite(sourcepump1, LOW);
  digitalWrite(uvlamp1, LOW);}

I don't see any problem with having the two if statements, although the else portion of the 2nd if statement will be executed whenever the condition is false, which is probably not what you want to happen with a pump.

There probably is no reason to test for > 0 in the first if statement, but I don't see it causing any problems unless the value jumps from 26 to 0.

Turn the pump and UV on if the distance is greater than 50.

Turn them off when the distance is zero. Maybe less than or equal to one so it doesn't overflow.

As you have it, the tank will never fill further than 25.

david_2018:
I don't see any problem with having the two if statements, although the else portion of the 2nd if statement will be executed whenever the condition is false, which is probably not what you want to happen with a pump.

There probably is no reason to test for > 0 in the first if statement, but I don't see it causing any problems unless the value jumps from 26 to 0.

So is it that I should remove the else statement?

If its 25 then the pump is low and only if it moves to 50 or above should it come on else it should be low thats why I included the else statement.

I dont want as soon as the water level drops the pump comes on then switches off then continues to do that.

So turn on at fifty and off when it's full. I don't see why there needs to be any consideration of twenty five at all.

I dont want it to be filled to the brim, also I dont want it to start again until its almost empty, how exactly would I code for that then.

wildbill:
if(distanceInch1 > 50) {
digitalWrite(sourcepump2, HIGH);
digitalWrite(uvlamp2, HIGH); }
if (distanceInch1 > 12) {
digitalWrite(sourcepump2, LOW);
digitalWrite(uvlamp2, LOW);

how does that look?

Change

if(distanceInch > 12) {

to

if(distanceInch <= 12) {

I have 3 ultrasonic sensors in 3 tanks and an arduino mega and an 8 channel relay board I need each tank to be filled a certain point and then shut off and only by going to a specific point it will start so that the pumps are constantly on and off.

this is the full code I have written, how does it look.

evanmars:
const int trigPin1=2;
const int echoPin1= 3;
const int trigPin2 =4;
const int echoPin2= 5;
const int trigPin3 =6;
const int echoPin3 =7;

#define sourcepump1 9
#define sourcepump2 10
#define transferpump1 29
#define transferpump2 33
#define ozonepump 11
#define uvlamp1 13
#define uvlamp2 12

long duration;
float distanceInch1;
float distanceInch2;
float distanceInch3;

void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(uvlamp1, OUTPUT);
pinMode(uvlamp2, OUTPUT);
pinMode(ozonepump, OUTPUT);
pinMode(transferpump1, OUTPUT);
pinMode(transferpump2, OUTPUT);
pinMode(sourcepump1, OUTPUT);
pinMode(sourcepump2, OUTPUT);
}

void loop() {
long duration1, distance1;
digitalWrite(trigPin1, LOW); // Added this line
delayMicroseconds(5); // Added this line
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distanceInch1 = (duration1/2) / 74;
Serial.print ( "SOURCEPUMP ");
Serial.print ( distance1);
delay(1000);

if(distanceInch1 > 50) {
digitalWrite(sourcepump1, HIGH);
digitalWrite(uvlamp1, HIGH); }
if (distanceInch1 <= 12) {
digitalWrite(sourcepump1, LOW);
digitalWrite(uvlamp1, LOW);

if(distanceInch1 > 50) {
digitalWrite(sourcepump2, HIGH);
digitalWrite(uvlamp2, HIGH);
if (distanceInch1 <= 12) {
digitalWrite(sourcepump2, LOW);
digitalWrite(uvlamp2, LOW);

long duration2, distance2;
digitalWrite(trigPin2, LOW); // Added this line
delayMicroseconds(5); // Added this line
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distanceInch2= (duration2/2) / 74;
Serial.print ( "TRANSFER PUMP ");
Serial.print ( distance2);
delay(1000);

if(distanceInch2 > 50) {
digitalWrite(transferpump1, HIGH);
if (distanceInch2 <= 12) {
digitalWrite(transferpump1, LOW);

if(distanceInch2 > 50) {
digitalWrite(transferpump2, HIGH);
if (distanceInch2 <= 12) {
digitalWrite(transferpump2, LOW);

long duration3, distance3;
digitalWrite(trigPin3, LOW); // Added this line
delayMicroseconds(5); // Added this line
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distanceInch3= (duration3/2) / 74;
Serial.print ( "OZONE TANK ");
Serial.print ( distance3);
delay(1000);

if(distanceInch3 > 50) {
digitalWrite(ozonepump, HIGH);

if (distanceInch3 <= 12) {
digitalWrite(ozonepump, LOW);

}}}}}}}}}}