// Start By THe Name Of Allah
// Compatible with the Arduino IDE 1.0
// Library version:1.1
#include <LiquidCrystal_I2C.h>
#define PIN_TRIG 3
#define PIN_ECHO 2
// set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27,20,4);
//Declaring the required variables
bool state,pump; //Pump
float cm; // Measured length
float L = 220.0; //Lenght
float D = 124.2; // Dia
float A = 19.6; // Dish distance
float R; //Radius
float H; // Liquid hight
float Ltr; // Total Liters
float Percentage; // % Liquid hight
void setup()
{
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("LEVEL SENSOR TM-V0.1");
lcd.setCursor(0,1);
lcd.print("DESIGND BY I_SHARIF");
lcd.setCursor(0,2);
lcd.print("TECHNICAL MINDS PAK");
lcd.setCursor(0,3);
lcd.print("+92 300/321 9408622");
delay(3000);
lcd.clear();
pinMode(13, OUTPUT);
}
void loop()
{
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
cm = (duration / 58.475);
R = D/2;
H = D + 2 - cm;
if(H<0)H=0;
Ltr = (L * ((sq(R) * acos(((R) - H) / (R))) - (((R) - H) * sqrt(2 * (R) * H - sq(H)))) + PI * A * sq(H) * (1 - (H / (3 * (R))))) / 1000;
Percentage = H * 100 / D;
//displaying the liquid level & liters
lcd.setCursor(0,0);
lcd.print("TANK LEVEL:");
lcd.setCursor(12, 0);
if(Percentage>90)lcd.print("TO HIGH");
else if(Percentage<20)lcd.print("TO LOW");
else if(Percentage<25)lcd.print("LOW");
else if(Percentage>25 && Percentage<85)lcd.print("NORMAL");
else if(Percentage>85)lcd.print("HIGH");
lcd.setCursor(0,1);
lcd.print("LIQUID HIGHT:");
lcd.setCursor(13,1);
lcd.print(H, 1);
lcd.print("cm");
lcd.setCursor(0, 2);
lcd.print("LITERS:");
lcd.setCursor(10, 2);
lcd.print(Ltr, 1);
lcd.print("Ltr");
//displaying the liquid level in percentage
lcd.setCursor(11, 3);
lcd.print("LH:");
lcd.setCursor(14, 3);
if(Percentage<0)Percentage=0;
lcd.print(Percentage, 1);
lcd.print("%");
// swtiching motor state based the read value
if(Percentage<30)pump=1;
if(Percentage>80)pump=0;
digitalWrite(13,pump);
//displaying the pump/motor state
lcd.setCursor(0, 3);
lcd.print("PUMP:");
lcd.setCursor(5, 3);
if(pump==1)lcd.print("ON ");
else if(pump==0) lcd.print("OFF");
delay(1000);
lcd.clear();
}