#include <LiquidCrystal.h> // Include the LiquidCrystal library
#include<Servo.h>
// LCD pin configuration
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
Servo s1;
// Ultrasonic sensor pins
const int trigPin = 13;
const int echoPin = 12;
// LED pin and motor control pins
const int led1 = 11;
const int led2 = 8;
const int ENA_PIN = 10;
const int IN1_PIN = 9;
const int IN2_PIN = 8;
// Variables for ultrasonic sensor
long duration;
float distanceCM;
float volume,waterpresent,waterlevel;
boolean motorRunning = false;
//int Dimeter=50;
int Hight=100;
void setup() {
// Initialize Serial Monitor for debugging
Serial.begin(9600);
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Water Level");
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize button pin and LED pin
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// Initialize motor control pins
// pinMode(ENA_PIN, OUTPUT);
/* pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);*/
s1.attach(13);
delay(500); // Small delay for LCD display to settle
}
void loop() {
// Calculate distance using ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo duration
duration = pulseIn(echoPin, HIGH);
distanceCM = duration * 0.034 / 2;
//calculet waterlevel
waterlevel = Hight - distanceCM;
// Limit value between 0 and tank height
if (waterlevel < 0)
waterlevel = 0;
if (waterlevel > Hight)
waterlevel = Hight;
//volume of tank
//volume =(3.142/4*pow(Dimeter,2)*waterlevel);
//Water present in the tank
waterpresent= (waterlevel * 100.0) / Hight;
// Display distance on LCD
lcd.setCursor(0, 0);
lcd.print("WaterPresent: ");
lcd.setCursor(0,1);
lcd.print(waterpresent);
lcd.print("% ");
// Display distance on serial monitor
Serial.print("Waterlevel: ");
Serial.print(waterlevel);
Serial.println(" cm ");
Serial.print("WaterPresent:");
Serial.print(waterpresent);
Serial.println(" ml\n");
// Always keep LED1 on
// If the distance is greater than 3 cm, trigger the motor and LED2
/*if (waterpresent <= 10)
{
// Run the motor
// digitalWrite(IN1_PIN, HIGH);
//dgitalWrite(IN2_PIN, LOW);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
for (int speed =0; speed <= 250; speed++)
{
analog,Write(speed); // Increase motor speed
delay(10);
} // Turn on LED2
delay(100); // Keep the motor running for 1 second
} else {
// If the distance is less than or equal to 3 cm, stop the motor
// analogWrite(ENA_PIN, 0); // Turn off motor speed
// digitalWrite(IN1_PIN, LOW);
//digitalWrite(IN2_PIN, LOW);
// Ensure LED1 is still on when motor is off
s1.write(0);
digitalWrite(led1, HIGH); // LED1 on
delay(500);
digitalWrite(led1, LOW);
}
*/ // Start when tank is 10% or below
if (waterpresent <= 10)
{
motorRunning = true;
// ON position
Serial.println("Servo ON");
}
// Stop when tank reaches 90%
if (waterpresent >= 90)
{
motorRunning = false;
Serial.println("Servo OFF");
}
// LED Indication
if (motorRunning)
{
s1.write(180);
delay(1000);
s1.write(0);
delay(10);
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
}
else
{
s1.write(0);
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
delay(1500); // Delay for 500ms before next loop iteration
}