1
2
3
4#include <LiquidCrystal_I2C.h>
5#include <Wire.h>
6LiquidCrystal_I2C lcd(0x27, 16, 2);
7
8
9
10
11#define sensorPower 7
12#define sensorPin A1
13#define trigPin 9
14#define echoPin 10
15#define pump_switch 3
16
17
18int waterval = 0;
19int ml = 0;
20long ultra_duration;
21int ultra_distance;
22
23void setup() {
24 Serial.begin(9600);
25
26 pinMode(sensorPower, OUTPUT);
27 pinMode(trigPin, OUTPUT);
28 pinMode(echoPin, INPUT);
29 pinMode(pump_switch,OUTPUT);
30
31
32 digitalWrite(sensorPower, LOW);
33 digitalWrite(pump_switch, LOW);
34
35 startlcd();
36}
37
38void loop() {
39
40 int sensorValue = analogRead(A0);
41
42
43 int level = readWaterSensor();
44 int days = sensorValue/16.7;
45 int presence = readUltraSensor();
46
47 Serial.print("Water level:");
48 Serial.println(level);
49
50 lcd.setCursor(6, 1);
51 lcd.print(days);
52 lcd.setCursor(13, 3);
53 lcd.print(level);
54
55 if (presence <= 5) {
56 lcd.setCursor(6, 2);
57 lcd.print("PUMP ON ");
58 digitalWrite(pump_switch, HIGH);
59 }
60
61 else {
62 lcd.setCursor(6, 2);
63 lcd.print("PUMP OFF");
64 digitalWrite(pump_switch, LOW);
65 }
66
67 delay(200);
68
69}
70
71
72int readWaterSensor() {
73 digitalWrite(sensorPower, HIGH);
74 delay(10);
75 waterval = analogRead(sensorPin);
76 waterval = waterval/2.2;
77 digitalWrite(sensorPower, LOW);
78 return waterval;
79}
80
81int readUltraSensor() {
82
83 digitalWrite(trigPin, LOW);
84 delayMicroseconds(2);
85
86 digitalWrite(trigPin, HIGH);
87 delayMicroseconds(10);
88 digitalWrite(trigPin, LOW);
89
90 ultra_duration = pulseIn(echoPin, HIGH);
91
92 ultra_distance= ultra_duration*0.034/2;
93
94 return ultra_distance;
95}
96
97void startlcd() {
98 lcd.init();
99 lcd.backlight();
100 lcd.begin(20, 4);
101 lcd.setCursor(4, 0);
102 lcd.print("INSERT TIME");
103 lcd.setCursor(8, 1);
104 lcd.print(" DAYS");
105 lcd.setCursor(0, 3);
106 lcd.print("Water Level:");
107 lcd.setCursor(17, 3);
108 lcd.print("ml");
109}
110