#include <Arduino.h>
#include <digitalWriteFast.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Define Pins
const int triggpin = 18;
const int echopin = 19;
const int pump = 13;
//Define Values
const float SPEED_OF_SOUND = 0.034 / 2; // In metre/microseconds
const int tanksensorheight = 95;
const int tankheight = 80;
const int tankradius = 45;
const int tanktriggerlevel = 87;
const int tanktriggeroff = 15;
const double pi = 3.1415926535897932384626433832795;
int lastDistance = -1;
long duration;
int distance;
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
intro();
// Set pin modes
pinMode(triggpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(pump, OUTPUT);
lcd.clear();
}
void loop()
{
ULSensor();
int waterheight;
if (distance <= 2) {
waterheight = tankheight;
} else if (distance >= tanksensorheight) {
waterheight = 0;
} else {
waterheight = tanksensorheight - distance;
if (waterheight > tankheight) {
waterheight = tankheight;
} else if (waterheight < 0) {
waterheight = 0;
}
}
float tankFilledPercentage = (waterheight / (float)tankheight) * 100;
if (distance != lastDistance)
{
Serial.println("------------------------------------");
Serial.print("Tank - Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Tank - Water Height: ");
Serial.print(waterheight);
Serial.println(" cm");
Serial.print("Tank - Tank Filled Percentage: ");
Serial.print(tankFilledPercentage);
Serial.println("%");
Serial.println("------------------------------------");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tank : ");
lcd.print(tankFilledPercentage);
lcd.print("%");
if (distance >= tanktriggerlevel) {
digitalWriteFast(pump, HIGH);
} else if (distance <= tanktriggeroff) {
digitalWriteFast(pump, LOW);
}
lastDistance = distance;
}
delay(500);
}
void intro() {
lcd.clear();
lcd.println("WATER MONITORING");
lcd.setCursor(3, 1);
lcd.println("SYSTEM:1.0");
delay(1500);
lcd.clear();
lcd.setCursor(7, 0);
lcd.println("ALL");
lcd.setCursor(1, 1);
lcd.println("RIGHTSRESERVED");
delay(750);
lcd.clear();
lcd.setCursor(1, 0);
lcd.println("MANOMOY MAITY");
delay(750);
}
void ULSensor() {
digitalWriteFast(triggpin, LOW);
delayMicroseconds(2);
digitalWriteFast(triggpin, HIGH);
delayMicroseconds(10);
digitalWriteFast(triggpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration * SPEED_OF_SOUND + 1;
}