/*This project is done to monitor the tank water level and to control the state of motor
based on the level.
A arduino board is used to control the automation. A ultrasonic sensor is used to
measure the distance. A rely module to control the current flow and Finally a lcd display
to show the water level and the pump state
Here a led is used to represent the motor since it cannot show in the simulation.
Water level is measured based on the distance between the sensor and water.
Means if the distance is more water level is less and vice versa since the sensor will be
fixed in the top side the tank.*/
//including the required libraries
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7); // initialising the lcd
//Declaring the required variables
long duration, inches;
int set_val,percentage;
bool state,pump;
//setting up the basic requirements.
void setup() {
lcd.begin(16, 2);//initialising the lcd
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP:OFF");
//initialising the pinmodes
pinMode(8, OUTPUT);
pinMode(9, INPUT);
pinMode(12, OUTPUT);
set_val=EEPROM.read(0); //reading the values
if(set_val>150)set_val=150;
}
void loop() {
//trigger
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(8, HIGH);
delayMicroseconds(10);
digitalWrite(8, LOW);
duration = pulseIn(9, HIGH);
inches = microsecondsToInches(duration);
percentage=(set_val-inches)*100/set_val;//calculating the water level percentage
//displaying the water level in percentage
lcd.setCursor(12, 0);
if(percentage<0)percentage=0;
lcd.print(percentage);
lcd.print("% ");
// swtiching motor state based the read value
if(percentage<30)pump=1;
if(percentage>95)pump=0;
digitalWrite(12,!pump);
//displaying the pump/motor state
lcd.setCursor(5, 1);
if(pump==1)lcd.print("ON ");
else if(pump==0) lcd.print("OFF");
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}