Automated water pump for household appliance

Recently I am working a project which is to run a 12volt water pump when the source tank is empty and reservoir tank is fully filled. The motor will pump the water from reservoir to source. I have written an arduino code. In my project I used two ldr for each water tank to sense the reflected ray from the water surface. I measured the value for each tank under empty and full condition. Then I used an led for indicating the pump’s activity. The problem arise when the pump starts it never stop. I rechecked my code but still unable to find the bug. Please help me. Itz my undergrade project and I am running out of time.

The arduino code:

int sensepin1=1;// reservoir sensor pin(blue wire)
int sensepin2=4;// source sensor pin(green wire)
int ledpinRF=5;//reservoir full led pin(Green led)
int ledpinRE=7;//reservoir empty led pin(RED led)
int ledpinSF=13;//reservoir full led pin(ORANGE led)
int ledpinSE=11;//reservoir empty led pin(red led)
int motor=2;//pump output pin(goes to pump driver IC)
int led=4;//pump running indicator led pin(WHITE led)

// for storing the sensor value in various variable
int val;//reservoir sensor min value
int bal;//reservoir sensor max value
int sal;//source sensor min value
int dal;//source sensor max value

//the variables for starting the pump
int kill;//reservoir sensor max value
int bill;//source sensor min value

void setup()
{
  analogReference(DEFAULT);
   
   pinMode(ledpinRF,OUTPUT);
   pinMode(ledpinRE,OUTPUT);
   pinMode(ledpinSF,OUTPUT);
   pinMode(ledpinSE,OUTPUT);
   pinMode(motor,OUTPUT);
   pinMode(led,OUTPUT);

}
void loop()
{
 
  val=analogRead(sensepin1);// read reservoir sensor value
  if(val<4 || val==4)//setting the value for minimum to indicate reservoir empty
  {
    digitalWrite(ledpinRE,HIGH);
     delay(200);
     digitalWrite(ledpinRE,LOW);
     delay(100);
  }
  else
  {
   digitalWrite(ledpinRE,LOW); 
  }
  bal=analogRead(sensepin1);//read the reservoir sensor value
  if (bal==8 || bal >8)//setting the value for max to indicate reservoir full
   {
     digitalWrite(ledpinRF,HIGH);
     delay(200);
     digitalWrite(ledpinRF,LOW);
     delay(100);
   } 
   else digitalWrite(ledpinRF,LOW);
   delay(200);
   
   sal= analogRead(sensepin2);//read the source sensor value
   if (sal<4 || sal==4)//setting the value for minimum to indicate source empty
   {
     digitalWrite(ledpinSE,HIGH);
     delay(200);
     digitalWrite(ledpinSE,LOW);
     delay(100);
   }
   else digitalWrite(ledpinSE,LOW);
   dal= analogRead(sensepin2);//read the source sensor value
   if (dal==8 || dal>8)//setting the value for maximum to indicate source full
   {
     digitalWrite(ledpinSF,HIGH);
     delay(200);
     digitalWrite(ledpinSF,LOW);
     delay(100);
   }
   else digitalWrite(ledpinSF,LOW);
   kill= analogRead(sensepin1);
   delay(200);
   bill=analogRead(sensepin2);
   delay(200);
   if (( kill>8 || kill==8) && (bill<4 || bill==4))//running the pump & pump led condition
   {
     int i=2;
     while(bill<7 && i>1)//here i>1 is always true but bill value is variable.i is used to continue the loop
   { 
     digitalWrite(led,HIGH);
     digitalWrite(motor,HIGH); 
     i++; 
     bill=analogRead(sensepin2);    
   } 
   digitalWrite(led,LOW);// after coming out of while loop this instruction will be executed
   digitalWrite(motor,LOW);
   }
  else                        //when the pump shouldn't be run
   {
     digitalWrite(motor,LOW);
     digitalWrite(led,LOW);
   }
  delay(100);
}

Moderator edit: CODE TAGS.

PLEASE read the sticky threads BEFORE posting.

 if(val<4 || val==4)

“<= 4” is so much more succinct.

Where are you debug prints?

int val;			//reservoir sensor min value
int bal;			//reservoir sensor max value
int sal;			//source sensor min value
int dal;			//source sensor max value

//the variables for starting the pump
int kill;			//reservoir sensor max value
int bill;			//source sensor min value

Cute variable names are great until you need to get help with your code. It seems a bit long too, given what it has to do.

thx 4 da comment. My program somehow got stuck inside the while loop and can't get out of it. Cz the code initially works when the both tank is empty by indicating red led for both tank. Then when the reservoir tank becomes full by external supply the pump starts to fill up the empty source. But when the source tank get filled the pump still continue running. I wonder why is this happening. I wrote the while loop for the pumping action. Can anybody say what is the problem of my code or the while loop? The clock is ticking for me. Plz help me..... :cold_sweat:

What are your debug prints telling you?

(Please don’t use txt)

when I compile the code it says ok. But still unable to solve the problem. I am looking for the logical error if is there any in my code. I wonder why my program stuck to the while loop(I guess) cz after starting the pump the white led goes flashing and never goes off until reset button is pressed. After pressing the reset button the program executed like source tank is filled up fully and reservoir is empty by flashing led. But my question is that why this thing is not happening before pressing reset button.

when I compile the code it says ok.

No doubt, but what do your debug prints tell you about the how the code is actually running?

(That's another Big Hint for you to add some debug prints to your code.
And please, stop using txt. When I see "Cz", I see the top-level domain for the Czech Republic)

I fixed your funky variable names so that I could understand your sketch and this resulting version seems very strange:

if ((ResReading >= 8) && (SrcReading <= 4))	//running the pump & pump led condition
  {
  int i = 2;
  while (SrcReading < 7 && i > 1)	//here i>1 is always true but SrcReading value is variable.i is used to continue the loop

Assuming I didn’t alter the logic as I transformed it, your LED control code seems to indicate that a reading of four or less means the vessel being measured is empty and eight or more means full. However, here, you’re looking for reservoir to be full and source being empty as a condition of running the pump. Backwards methinks.

I’m a little dubious about the 4 & 8 being accurate too - that’s a mighty small range to rely on from analogRead.