#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
long duration, inches;
int set_val,percentage;
bool state,pump;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP:OFF MANUAL");
pinMode(14, OUTPUT);
pinMode(27, INPUT);
pinMode(26, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(5, OUTPUT);
set_val=EEPROM.read(0);
if(set_val>150)set_val=150;
}
void loop() {
analogRead(A3);
delayMicroseconds(2);
digitalWrite(14, HIGH);
delayMicroseconds(10);
digitalWrite(14, LOW);
duration = pulseIn(27, HIGH);
inches = microsecondsToInches(duration);
percentage=(set_val-inches)*100/set_val;
lcd.setCursor(12, 0);
if(percentage<0)percentage=0;
lcd.print(percentage);
lcd.print("% ");
if(percentage<30&digitalRead(25))pump=1;
if(percentage>99)pump=0;
digitalWrite(5,!pump);
lcd.setCursor(5, 1);
if(pump==1)lcd.print("ON ");
else if(pump==0) lcd.print("OFF");
lcd.setCursor(9, 1);
if(!digitalRead(25))lcd.print("MANUAL");
else lcd.print("AUTO ");
if(!digitalRead(26)&!state&digitalRead(25)){
state=1;
set_val=inches;
EEPROM.write(0, set_val);
}
if(!digitalRead(26)&!state&!digitalRead(25)){
state=1;
pump=!pump;
}
if(digitalRead(26))state=0;
delay(500);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}