#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
long duration, inches;
int set_val,percentage;
bool alarm_state,pump1,pump2,valve;
void setup() {
lcd.begin(20, 4);
lcd.print("LEVEL :");
lcd.setCursor(0, 1);
lcd.print("MODE :LEAD A ");
lcd.setCursor(0, 2);
lcd.print("VALVE : OPEN ");
lcd.setCursor(0, 3);
lcd.print("STATUS :NORMAL ");
pinMode(0, OUTPUT);
pinMode(1, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
set_val=EEPROM.read(0);
if(set_val>150)set_val=150;
}
void loop() {
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(0, HIGH);
delayMicroseconds(10);
digitalWrite(0, LOW);
duration = pulseIn(1, HIGH);
inches = microsecondsToInches(duration);
percentage=(set_val-inches)*100/set_val;
lcd.setCursor(8, 0);
if(percentage<0)percentage=0;
lcd.print(percentage);
lcd.print("% ");
if (percentage>=90){
alarm_state=1;
digitalWrite(8, HIGH);
}
if (percentage<=40&!digitalRead(10)){
alarm_state=0;
digitalWrite(8, LOW);
}
// LEAD A
if(percentage>=60&!alarm_state&!digitalRead(11))pump1=1;
if(percentage>=70&!alarm_state&!digitalRead(11))pump2=1;
if(percentage<=40&!alarm_state&!digitalRead(11)){
pump1=0;
pump2=0;
valve=1;
}
if(percentage>=90&!digitalRead(11)){
pump1=0;
pump2=0;
valve=0;
}
digitalWrite(12, !pump1);
digitalWrite(9, !pump2);
digitalWrite(13, !valve);
// LEAD B
if(percentage>=60&!alarm_state&digitalRead(11))pump2=1;
if(percentage>=70&!alarm_state&digitalRead(11))pump1=1;
if(percentage<=40&!alarm_state&digitalRead(11)){
pump1=0;
pump2=0;
valve=1;
}
if(percentage>=90&digitalRead(11)){
pump2=0;
pump1=0;
valve=0;
}
digitalWrite(12, !pump1);
digitalWrite(9, !pump2);
digitalWrite(13, !valve);
lcd.setCursor(8, 3);
if(percentage>=70)lcd.print("LEVEL HIGH");
else lcd.print("NORMAL ");
lcd.setCursor(8, 3);
if(percentage>=90) lcd.print("ALARM ");
lcd.setCursor(8, 2);
if(valve)lcd.print("OPEN ");
else lcd.print("CLOSE");
lcd.setCursor(8, 1);
if(!digitalRead(11))lcd.print("LEAD A");
else lcd.print("LEAD B ");
delay(500);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}