1#include <EEPROM.h>
2
3#include <LiquidCrystal.h>
4LiquidCrystal lcd(2,3,4,5,6,7);
5
6long duration, inches;
7int set_val,percentage;
8bool state,pump;
9
10
11void setup() {
12
13 lcd.begin(16, 2);
14 lcd.print("WATER LEVEL:");
15 lcd.setCursor(0, 1);
16 lcd.print("PUMP:OFF MANUAL");
17
18 pinMode(8, OUTPUT);
19 pinMode(9, INPUT);
20 pinMode(10, INPUT_PULLUP);
21 pinMode(11, INPUT_PULLUP);
22 pinMode(12, OUTPUT);
23
24 set_val=EEPROM.read(0);
25 if(set_val>150)set_val=150;
26}
27void loop() {
28
29 digitalWrite(3, LOW);
30 delayMicroseconds(2);
31 digitalWrite(8, HIGH);
32 delayMicroseconds(10);
33 digitalWrite(8, LOW);
34 duration = pulseIn(9, HIGH);
35 inches = microsecondsToInches(duration);
36
37 percentage=(set_val-inches)*100/set_val;
38
39 lcd.setCursor(12, 0);
40 if(percentage<0)percentage=0;
41 lcd.print(percentage);
42 lcd.print("% ");
43
44 if(percentage<30&digitalRead(11))pump=1;
45 if(percentage>99)pump=0;
46 digitalWrite(12,!pump);
47
48 lcd.setCursor(5, 1);
49 if(pump==1)lcd.print("ON ");
50 else if(pump==0) lcd.print("OFF");
51
52 lcd.setCursor(9, 1);
53 if(!digitalRead(11))lcd.print("MANUAL");
54 else lcd.print("AUTO ");
55
56 if(!digitalRead(10)&!state&digitalRead(11)){
57 state=1;
58 set_val=inches;
59 EEPROM.write(0, set_val);
60 }
61
62 if(!digitalRead(10)&!state&!digitalRead(11)){
63 state=1;
64 pump=!pump;
65
66 }
67
68 if(digitalRead(10))state=0;
69
70
71 delay(500);
72}
73long microsecondsToInches(long microseconds) {
74 return microseconds / 74 / 2;
75}
76