Help needed to complete full automatic washing machine controller.

I'm new to arduino programming so I can't figure out few things so kindly help me to complete this project.

I have written the code by referring Aruduino tutorials and projects found in the internet. The algorithm works as follows.

(Fill>Wash>Drain>Dry)>(Fill>Rinse>Drain>Dry)>(Fill>Rinse>Drain>Dry)>(Fill>Rinse>Drain)>Spindry

The code I have written skips the "Fill" part in every cycle. First it checks for the water level sensor reading. If the reading is LOW, it starts to fill the tub until the sensor reading becomes HIGH. The water level sensor is the one that came with the washing machine.

As I have read it is nothing but a pressure actuated switch so I used a push button and a 100k resistor to replicate the sensor when testing the program.

It seems like the arduino does not wait for the sensor reading. It simply jumps to the wash phase. :frowning:

Also I need the program to check for the door sensor and level sensor (these sensors are also similar to push buttons) in each phase.

Hers's the code.

#define waterInlet 5
#define drainValve 4
#define forMotor 3
#define revMotor 2
#define goButton A1
#define wlSensor 7
#define goLight 0
#define stopLight 1
#define stopButton A3
#define doorSensor A0
 
void setup () {
  
  pinMode(waterInlet, OUTPUT);
  pinMode(drainValve, OUTPUT);
  pinMode(forMotor, OUTPUT);
  pinMode(revMotor, OUTPUT);
  pinMode(goButton, INPUT);
  pinMode(stopButton, INPUT);
  digitalWrite(stopButton, HIGH);
  pinMode(wlSensor, INPUT_PULLUP);
  pinMode(doorSensor, INPUT);
  digitalWrite(doorSensor, HIGH);
  pinMode(goLight, OUTPUT);
  pinMode(stopLight, OUTPUT);
}
 
void fillItUp() {
  if (digitalRead(wlSensor) == LOW)
     {
     digitalWrite(waterInlet, HIGH);
     delay (1000);
     }
     else {
       digitalWrite(waterInlet, LOW);
       delay(1000);
     }
}
 
void drainItOut() {
  unsigned long drainTime = millis();
  digitalWrite(drainValve, HIGH);
  delay(500);
  while ((millis() - drainTime) < 100000) {  // Assuming that the water drains within 100 seconds.
    delay(100);
  }
  digitalWrite(drainValve, LOW);
  delay(100);
}

void washPhase() {
  for (int i=0; i < 20; i++){
  digitalWrite(forMotor, HIGH);
  delay(5000);
  digitalWrite(forMotor, LOW);
  delay(1000);
  digitalWrite(revMotor, HIGH);
  delay(5000);
  digitalWrite(revMotor, LOW);
  delay(1000);
  }
}
 
void ppreDry() {
  digitalWrite(drainValve, HIGH);
  delay(1000);
  digitalWrite(forMotor, HIGH);
  delay(5000);
  digitalWrite(forMotor, LOW);
  delay(2000);
  digitalWrite(forMotor, HIGH);
  delay(7000);
  digitalWrite(forMotor, LOW);
  delay(2000);
  digitalWrite(forMotor, HIGH);
  delay(10000);
  digitalWrite(forMotor, LOW);
  delay(2000);
}

void preDry() {
  unsigned long dryTime = millis();
  digitalWrite(forMotor, HIGH);
  delay(500);
  while ((millis() - dryTime) < 120000) { // pre dry for 120 seconds
    delay(500);
  }
  digitalWrite(forMotor, LOW);
  digitalWrite(drainValve, LOW);
  delay(500);
}
 
void finalDry() {
  unsigned long fdryTime = millis();
  digitalWrite(forMotor, HIGH);
  delay(500);
  while ((millis() - fdryTime) < 300000) { // final dry for 300 seconds
    delay(500);
  }
  digitalWrite(forMotor, LOW);
  digitalWrite(drainValve, LOW);
  delay(500);
}

void loop() {

  // Go button was pressed, run through the cycles
  digitalWrite(goLight, HIGH);
  digitalWrite(stopLight, HIGH);
  // Washing
  fillItUp();
  delay(1000);
  washPhase();
  delay(1000);
  drainItOut();
  delay(1000);
  ppreDry();
  delay(1000);
  preDry();
  delay(1000);
  // Rinsing 1
  fillItUp();
  delay(1000);
  washPhase();
  delay(1000);
  drainItOut();
  delay(1000);
  ppreDry();
  delay(1000);
  preDry();
  delay(1000);
  // Rinsing 2
  fillItUp();
  delay(1000);
  washPhase();
  delay(1000);
  drainItOut();
  delay(1000);
  //Spin dry
  ppreDry();
  delay(1000);
  preDry();
  delay(1000);
  finalDry();
  digitalWrite(goLight, LOW);
  digitalWrite(stopLight, HIGH);
}

Thanks in advance.

It seems like the arduino does not wait for the sensor reading.

Of course not. Why do you expect it to? If you want the Arduino to wait for something to happen, you have to program it to wait.

PaulS:
Of course not. Why do you expect it to? If you want the Arduino to wait for something to happen, you have to program it to wait.

:smiley:

I have no idea how to do that. Please help me to sort it out. :frowning:
Is it possible with dowhile?
http://www.arduino.cc/en/Reference/DoWhile
Thanks

Is it possible with dowhile?

Yes, but a while statement is better.

If digitalRead(somePin) returns LOW when the switch is pressed, and HIGH when it is not pressed (as it should, if the switch is wired correctly), you can wait for a switch to become pressed:

while(digitalRead(somePin) == HIGH)
{
   // Do nothing...
}

PaulS:
Yes, but a while statement is better.

If digitalRead(somePin) returns LOW when the switch is pressed, and HIGH when it is not pressed (as it should, if the switch is wired correctly), you can wait for a switch to become pressed:

while(digitalRead(somePin) == HIGH)

{
  // Do nothing...
}

It works. Thank you very much!

:slight_smile:

Please help me to set modes for a program using a push button.

It has to be like this. In a washing machine we can select individual modes such as Wash, Rinse, SpinDry at the very beginning. I need to achieve a similar thing in my code. It is for a washing machine control unit.

I tried may things such as using a counter, while loop, etc...

Almost all the example codes found here are based on realtime mode switching. (Changing brightness of a LED, Changing number of blinks etc). I need to set the mode at the beginning.

In other words it has to follow the following path:

Power On> Arduino waits for the Input> (One Button Push = Wash, Two Rinse, Three = SpinDry)> Hit the Start Button> Arduino completes the given task.

This is my code:

#define waterInlet 5
#define drainValve 4
#define forMotor 3
#define revMotor 2
#define goButton 8
#define wlSensor 6
#define goLight 0
#define stopLight 1
#define stopButton A3
#define doorSensor A0
 
void setup () {
  
  pinMode(waterInlet, OUTPUT);
  pinMode(drainValve, OUTPUT);
  pinMode(forMotor, OUTPUT);
  pinMode(revMotor, OUTPUT);
  pinMode(goButton, INPUT);
  pinMode(stopButton, INPUT);
  digitalWrite(stopButton, HIGH);
  pinMode(wlSensor, INPUT);
  pinMode(doorSensor, INPUT);
  digitalWrite(doorSensor, HIGH);
  pinMode(goLight, OUTPUT);
  pinMode(stopLight, OUTPUT);
  digitalWrite(forMotor, HIGH);
  digitalWrite(revMotor, HIGH);
  digitalWrite(waterInlet, LOW);
  digitalWrite(drainValve, LOW);
  delay(2000);
}

void fillItUp() {
  digitalWrite(wlSensor, HIGH);
 delay(5000); 
  while(digitalRead(wlSensor) == HIGH)
  {
    digitalWrite(waterInlet, HIGH);
    delay(1000);
  }
  digitalWrite(waterInlet, LOW);
  delay(1000);
}
 
void drainItOut() {
  unsigned long drainTime = millis();
  digitalWrite(drainValve, HIGH);
  delay(500);
  while ((millis() - drainTime) < 90000) {  // Assuming that the water drains within 900 seconds.
    delay(500);
  }
  digitalWrite(drainValve, LOW);
}

void washPhase() {
  for (int i=0; i < 20; i++){
  digitalWrite(forMotor, LOW);
  delay(3000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
  digitalWrite(revMotor, LOW);
  delay(3000);
  digitalWrite(revMotor, HIGH);
  delay(2000);
  }
}

void rinsePhase() {
  for (int i=0; i < 10; i++){
  digitalWrite(forMotor, LOW);
  delay(3000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
  digitalWrite(revMotor, LOW);
  delay(3000);
  digitalWrite(revMotor, HIGH);
  delay(2000);
  }
}
 
void ppreDry() {
  digitalWrite(drainValve, HIGH);
  delay(1000);
  digitalWrite(forMotor, LOW);
  delay(4000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
  digitalWrite(forMotor, LOW);
  delay(6000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
}

void preDry() {
  unsigned long dryTime = millis();
  digitalWrite(forMotor, LOW);
  delay(500);
  while ((millis() - dryTime) < 60000) { // pre dry for 60 seconds
    delay(500);
  }
  digitalWrite(forMotor, HIGH);
  delay(45000);
  digitalWrite(drainValve, LOW);
  delay(500);
}
 
void finalDry() {
  unsigned long fdryTime = millis();
  digitalWrite(forMotor, LOW);
  delay(500);
  while ((millis() - fdryTime) < 180000) { // final dry for 180 seconds
    delay(500);
  }
  digitalWrite(forMotor, HIGH);
  delay(45000);
  digitalWrite(drainValve, LOW);
  delay(500);
}

void loop() {

  // Go button was pressed, run through the cycles
  digitalWrite(goLight, HIGH);
  digitalWrite(stopLight, HIGH);
  // Washing
  fillItUp();
  delay(1000);
  washPhase();
  delay(1000);
  drainItOut();
  ppreDry();
  preDry();
  delay(1000);
  // Rinsing 1
  fillItUp();
  delay(1000);
  rinsePhase();
  delay(1000);
  drainItOut();
  ppreDry();
  preDry();
  delay(1000);
  // Rinsing 2
  fillItUp();
  delay(1000);
  rinsePhase();
  delay(1000);
  drainItOut();
  // Spin dry
  ppreDry();
  finalDry();
  delay(1000);
  digitalWrite(goLight, LOW);
  digitalWrite(stopLight, HIGH);
  while(1) { }
}

Thanks in advance!

Please help me to set modes for a program using a push button.

It has to be like this. In a washing machine we can select individual modes such as Wash, Rinse, SpinDry at the very beginning. I need to achieve a similar thing in my code. It is for a washing machine control unit.

I tried may things such as using a counter, while loop, etc...

Almost all the example codes found here are based on realtime mode switching. (Changing brightness of a LED, Changing number of blinks etc). I need to set the mode at the beginning.

In other words it has to follow the following path:

Power On> Arduino waits for the Input> (One Button Push = Wash, Two Rinse, Three = SpinDry)> Hit the Start Button> Arduino completes the given task.

This is my code:

#define waterInlet 5
#define drainValve 4
#define forMotor 3
#define revMotor 2
#define goButton 8
#define wlSensor 6
#define goLight 0
#define stopLight 1
#define stopButton A3
#define doorSensor A0
 
void setup () {
  
  pinMode(waterInlet, OUTPUT);
  pinMode(drainValve, OUTPUT);
  pinMode(forMotor, OUTPUT);
  pinMode(revMotor, OUTPUT);
  pinMode(goButton, INPUT);
  pinMode(stopButton, INPUT);
  digitalWrite(stopButton, HIGH);
  pinMode(wlSensor, INPUT);
  pinMode(doorSensor, INPUT);
  digitalWrite(doorSensor, HIGH);
  pinMode(goLight, OUTPUT);
  pinMode(stopLight, OUTPUT);
  digitalWrite(forMotor, HIGH);
  digitalWrite(revMotor, HIGH);
  digitalWrite(waterInlet, LOW);
  digitalWrite(drainValve, LOW);
  delay(2000);
}

void fillItUp() {
  digitalWrite(wlSensor, HIGH);
 delay(5000); 
  while(digitalRead(wlSensor) == HIGH)
  {
    digitalWrite(waterInlet, HIGH);
    delay(1000);
  }
  digitalWrite(waterInlet, LOW);
  delay(1000);
}
 
void drainItOut() {
  unsigned long drainTime = millis();
  digitalWrite(drainValve, HIGH);
  delay(500);
  while ((millis() - drainTime) < 90000) {  // Assuming that the water drains within 900 seconds.
    delay(500);
  }
  digitalWrite(drainValve, LOW);
}

void washPhase() {
  for (int i=0; i < 20; i++){
  digitalWrite(forMotor, LOW);
  delay(3000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
  digitalWrite(revMotor, LOW);
  delay(3000);
  digitalWrite(revMotor, HIGH);
  delay(2000);
  }
}

void rinsePhase() {
  for (int i=0; i < 10; i++){
  digitalWrite(forMotor, LOW);
  delay(3000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
  digitalWrite(revMotor, LOW);
  delay(3000);
  digitalWrite(revMotor, HIGH);
  delay(2000);
  }
}
 
void ppreDry() {
  digitalWrite(drainValve, HIGH);
  delay(1000);
  digitalWrite(forMotor, LOW);
  delay(4000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
  digitalWrite(forMotor, LOW);
  delay(6000);
  digitalWrite(forMotor, HIGH);
  delay(2000);
}

void preDry() {
  unsigned long dryTime = millis();
  digitalWrite(forMotor, LOW);
  delay(500);
  while ((millis() - dryTime) < 60000) { // pre dry for 60 seconds
    delay(500);
  }
  digitalWrite(forMotor, HIGH);
  delay(45000);
  digitalWrite(drainValve, LOW);
  delay(500);
}
 
void finalDry() {
  unsigned long fdryTime = millis();
  digitalWrite(forMotor, LOW);
  delay(500);
  while ((millis() - fdryTime) < 180000) { // final dry for 180 seconds
    delay(500);
  }
  digitalWrite(forMotor, HIGH);
  delay(45000);
  digitalWrite(drainValve, LOW);
  delay(500);
}

void loop() {

  // Go button was pressed, run through the cycles
  digitalWrite(goLight, HIGH);
  digitalWrite(stopLight, HIGH);
  // Washing
  fillItUp();
  delay(1000);
  washPhase();
  delay(1000);
  drainItOut();
  ppreDry();
  preDry();
  delay(1000);
  // Rinsing 1
  fillItUp();
  delay(1000);
  rinsePhase();
  delay(1000);
  drainItOut();
  ppreDry();
  preDry();
  delay(1000);
  // Rinsing 2
  fillItUp();
  delay(1000);
  rinsePhase();
  delay(1000);
  drainItOut();
  // Spin dry
  ppreDry();
  finalDry();
  delay(1000);
  digitalWrite(goLight, LOW);
  digitalWrite(stopLight, HIGH);
  while(1) { }
}

Thanks in advance!

In other words it has to follow the following path:

You want to separate reading the mode switch from reading the start switch. You can use the state change detection example to learn when the mode switch has become pressed. Each time that happens, increment a variable.

You can use the state change detection example to learn when the start switch has become pressed. When that happens, use the value of the mode variable to determine what to do.

Thanks :slight_smile:

I came up with this code.

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
    }
    else {}
    delay(100);
  }
  lastButtonState = buttonState;
  if (buttonPushCounter % 3 == 0) {
      digitalWrite(washLed, HIGH);
      digitalWrite(rinseLed, HIGH);
      digitalWrite(dryLed, HIGH);
      delay(100);
    }
  else if (buttonPushCounter % 3 == 2) {
    digitalWrite(washLed, LOW);
    digitalWrite(rinseLed, HIGH);
    digitalWrite(dryLed, LOW);
  }
  else if (buttonPushCounter % 3 == 1) {
    digitalWrite(washLed, LOW);
    digitalWrite(rinseLed, LOW);
    digitalWrite(dryLed, HIGH);
  }

//gobutton is pressed

  digitalWrite(goButton, HIGH);
  delay(100); 
  while(digitalRead(goButton) == HIGH)
  {
    if (buttonPushCounter % 3 == 0)
    {
      wash();
      delay(100);
    }
     else if (buttonPushCounter % 3 == 1)
    {
      rinse();
      delay(100);
    }
     else if (buttonPushCounter % 3 == 2)
    {
      dry();
      delay(100);
      }
    }
}

I think I'm missing something. The buttonPushCounter doesn't work after adding the gobutton code. Please help me to solve this puzzle :frowning:

Thanks once again PaulS :slight_smile:

  digitalWrite(goButton, HIGH);

Why are you writing to an INPUT pin?

  while(digitalRead(goButton) == HIGH)

If it isn't an INPUT pin, why are you reading it?

  if (buttonPushCounter % 3 == 0) {
  else if (buttonPushCounter % 3 == 2) {
  else if (buttonPushCounter % 3 == 1) {

0, 2, 1? WTF?

No washing machine that I have ever used fills in one second. Does yours ?

PaulS:

  digitalWrite(goButton, HIGH);

Why are you writing to an INPUT pin?

  while(digitalRead(goButton) == HIGH)

If it isn't an INPUT pin, why are you reading it?

  if (buttonPushCounter % 3 == 0) {

else if (buttonPushCounter % 3 == 2) {
 else if (buttonPushCounter % 3 == 1) {



0, 2, 1? WTF?

Rectified those errors. Still I don't have any luck :frowning:

I can switch from first mode (wash) to second mode (rinse) if I press and hold the button when it boots up. As soon as it boots up it starts the cycle. So there's no way to change the mode afterwords. I need the Arduino to wait until my input. Let's say 10 seconds? Tried to implement this with while loop. It seems like it doesn't work :frowning:

New code:

void loop() {
 buttonState = digitalRead(buttonPin);
 if (buttonState != lastButtonState) {
   if (buttonState == HIGH) {
     buttonPushCounter++;
   }
 }
 lastButtonState = buttonState;
 if (buttonPushCounter % 3 == 0) {
     digitalWrite(washLed, HIGH);
     digitalWrite(rinseLed, HIGH);
     digitalWrite(dryLed, HIGH);
     delay(100);
   }
 else if (buttonPushCounter % 3 == 1) {
   digitalWrite(washLed, LOW);
   digitalWrite(rinseLed, HIGH);
   digitalWrite(dryLed, LOW);
 }
 else if (buttonPushCounter % 3 == 2) {
   digitalWrite(washLed, LOW);
   digitalWrite(rinseLed, LOW);
   digitalWrite(dryLed, HIGH);
 } 
 while(digitalRead(goButton) == HIGH)
 {
   if (buttonPushCounter % 3 == 0)
   {
     wash();
     delay(100);
   }
    else if (buttonPushCounter % 3 == 1)
   {
     rinse();
     delay(100);
   }
    else if (buttonPushCounter % 3 == 2)
   {
     dry();
     delay(100);
     }
   }
}

michinyon:
No washing machine that I have ever used fills in one second. Does yours ?

:smiley:

No it waits for the water level sensor. Delay is used for debounce. :slight_smile:

void fillItUp() {
  if (digitalRead(wlSensor) == LOW)
     {
     digitalWrite(waterInlet, HIGH);
     delay (1000);
     }
     else {
       digitalWrite(waterInlet, LOW);
       delay(1000);
     }
}

So, you call this function.

You read the state of the sensor, then turn the water inlet on, or off, then wait 1 second, then leave.

Your code in loop( ) which calls this function, only calls this function once.

Now, supposing that wlSensor == LOW means the tank is empty , or not full enough, ( I dunno, did you test the sensor ? ), then what this will do is turn the tap on, wait one second, and then go back to loop( ). AND NEVER TURN THE TAP OFF.

Have you ever seen a washing machine ?

You need logic which looks more like this

void fillItUp() 
{
  bool inlet_state = false ;           //  true,  when the water inlet solenoid valve is open
  unsigned long start_time= 0 ;    //  remember when you turn the water on
  bool finished = false ;
  
  while( ! finished )
  {
      if (digitalRead(wlSensor) == LOW)      //  not enough water yet
      {
           if ( !inlet_state )
           {                                            // turn the water on
               inlet_state = true ;
               start_time = millis() ;
               digitalWrite(waterInlet, HIGH);
               delay (1000);
           }
           else    // water is already on
           {
                 if ((millis()-start_time > 300000UL )
                {
                      // It has been filling for more than 5 minutes,  give up
                      digitalWrite(waterInlet, LOW );
                      finished = true ;
                }
                delay(1000);
           }
      }
      else  // sensor says its full
      {
            digitalWrite( waterInlet, LOW );
            finished = true ;
      }
  
   }  // end while

}

No it waits for the water level sensor.

What part of your code do you think it "waits" for the sensor.

Instead of all these delays, you would be better off to use a "state model", where the progress from one stage of the process depends on sensors as well as elapsed time.

With your scheme of using delay( ) all over the places, you can never check more than one thing at a time.

michinyon:

void fillItUp() {

if (digitalRead(wlSensor) == LOW)
    {
    digitalWrite(waterInlet, HIGH);
    delay (1000);
    }
    else {
      digitalWrite(waterInlet, LOW);
      delay(1000);
    }
}




So, you call this function.

You read the state of the sensor, then turn the water inlet on, or off, then wait 1 second, then leave.

Your code in loop( ) which calls this function, only calls this function once.

Now, supposing that wlSensor == LOW means the tank is empty , or not full enough, ( I dunno, did you test the sensor ? ), then what this will do is turn the tap on, wait one second, and then go back to loop( ). AND NEVER TURN THE TAP OFF.

Have you ever seen a washing machine ?

You need logic which looks more like this




void fillItUp()
{
  bool inlet_state = false ;          //  true,  when the water inlet solenoid valve is open
  unsigned long start_time= 0 ;    //  remember when you turn the water on
  bool finished = false ;
 
  while( ! finished )
  {
      if (digitalRead(wlSensor) == LOW)      //  not enough water yet
      {
          if ( !inlet_state )
          {                                            // turn the water on
              inlet_state = true ;
              start_time = millis() ;
              digitalWrite(waterInlet, HIGH);
              delay (1000);
          }
          else    // water is already on
          {
                if ((millis()-start_time > 300000UL )
                {
                      // It has been filling for more than 5 minutes,  give up
                      digitalWrite(waterInlet, LOW );
                      finished = true ;
                }
                delay(1000);
          }
      }
      else  // sensor says its full
      {
            digitalWrite( waterInlet, LOW );
            finished = true ;
      }
 
  }  // end while

}

Thanks. I replaced my code with this :slight_smile:

michinyon:
What part of your code do you think it "waits" for the sensor.

Instead of all these delays, you would be better off to use a "state model", where the progress from one stage of the process depends on sensors as well as elapsed time.

With your scheme of using delay( ) all over the places, you can never check more than one thing at a time.

Sorry. I have added a wrong code here. It was modified as PaulS's instructions. The while loop checks for the water level sensor. Anyway your code seems solid so I replaced my code with it.

Previously I used this code and it worked.

void fillItUp() { 
  while(digitalRead(wlSensor) == HIGH)
  {
    digitalWrite(waterInlet, HIGH);
    delay(1000);
  }
  digitalWrite(waterInlet, LOW);
  delay(1000);
}

Hi, I am trying to make the same project. I have problems with the motor. can you tell me how you solve the motor driving.

Thanks

Note that newer water sensors are nor switches anymore, they have IC and return a signal that is a square wave whose frequency depends on the air pressure inside the tube.
pins are GND, +5v, signal
I have some of that sensors and all start at about 25KHz and decrease as the pressure increases.
I discoverer the pinout with a cheap multimeter: in the 200 Ohm range, resistance appears for a second or so between the positive and negative. My tester in Ohm position has reversed polarity: it delivers + from the common cable so i knew wicht one is the positive and negative.
I could read clearly 5 water levels.
As it is a square wave you can read the High Time and Low time, and figure the frequency, using the pulseIn() function like this (reads m time to make mean time as readings vary a lot).
I had the signal connected to the D2 pin

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
float y=9;
void setup()
{
lcd.init(); // initialize the lcd
lcd.init();

lcd.backlight();
lcd.setCursor(3,0);
pinMode(2,INPUT);
}

long htime;
long ltime;
long freq;
float t=0;
int level;
int fmaxx = 25380;
int fminn = 20000;
int m=500;
void loop()
{ lcd.setCursor(121,0);
lcd.print("m=");
lcd.print(m);
lcd.print(" ");
delay(500);
lcd.clear();
htime=pulseIn(2,HIGH);
ltime=pulseIn(2,LOW);
t=0;
for (int i=0;i<m;i++) {
t=t+(pulseIn(2,HIGH)+pulseIn(2,LOW))*(1.000/m);
}

lcd.setCursor(0,0);
lcd.setCursor(0,1);
freq= 1000000.0 / t;

lcd.print("f=");
lcd.print(freq);
lcd.print("Hz ");

level = map( constrain(freq, fminn,fmaxx),fminn,fmaxx,100,0);
lcd.setCursor(0,0);
lcd.print("Nivel=");
lcd.print(level);
lcd.print(" ");

delay(500);

}

You need add water level switch or sensor easy way is switch but not perfect need use sensor same your upload sensor is working frequency 1650hz(18l) to 2500hz (48) so your code need to add this but i don't know how to create code help me i need this code