#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(10, 11, 12);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
long duration, distance;
int percentage;
const int push = 5;
const int trigpin = 2;
const int echopin = 4;
const int relay = 10;
void setup() {
lcd.init();
lcd.backlight();
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP:OFF MANUAL");
pinMode(relay, OUTPUT);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(push, INPUT);
// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
//myRTC.setDS1302Time(15, 26, 14, 7, 15, 4, 2023); //Here you write your actual time/date as shown above
//but remember to "comment/remove" this function once you're done
//The setup is done only one time and the module will continue counting it automatically
/*lcd.clear(); //Here after clearing the LCD we take the time from the module and print it on the screen with usual LCD functions
myRTC.updateTime();
lcd.setCursor(0, 0);
lcd.print("date: ");
lcd.print(myRTC.dayofmonth);
lcd.print("/");
lcd.print(myRTC.month);
lcd.print("/");
lcd.print(myRTC.year);
lcd.setCursor(0, 1);
lcd.print("time: ");
lcd.print(myRTC.hours);
lcd.print(":");
lcd.print(myRTC.minutes);
lcd.print(":");
lcd.print(myRTC.seconds);
lcd.setCursor(0, 1);
lcd.print("Water Level");
delay(1000);*/
}
void loop() {
int set_val = 200;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration * 0.034 / 2;
percentage = ((set_val - distance) * 100) / set_val;
lcd.clear(); //Here after clearing the LCD we take the time from the module and print it on the screen with usual LCD functions
lcd.setCursor(0, 0);
lcd.print("water level: ");
lcd.print(percentage);
if (percentage < 30) {
digitalWrite(relay, LOW);
} else if (percentage > 95) {
digitalWrite(relay, HIGH);
}
}