-
Notifications
You must be signed in to change notification settings - Fork 0
/
Water_tank_automation.ino
59 lines (51 loc) · 1.73 KB
/
Water_tank_automation.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <NewPing.h>
#include <SoftwareSerial.h>
// Pin definitions
#define TRIGGER_PIN 9
#define ECHO_PIN 10
#define RELAY_PIN 2
#define BLUETOOTH_TX 4
#define BLUETOOTH_RX 5
// Constants
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters)
#define LOWER_THRESHOLD 2 // Lower water level threshold (in centimeters)
#define UPPER_THRESHOLD 11 // Upper water level threshold (in centimeters)
// Objects
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX);
void setup() {
Serial.begin(115200);
bluetooth.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure the relay is off initially
Serial.println("Water Tank Automation System Initialized");
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec)
unsigned int distance = sonar.ping_cm();
if (distance > 0) {
Serial.print("Water Level: ");
Serial.print(distance);
Serial.println(" cm");
if (distance <= LOWER_THRESHOLD) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the water pump
Serial.println("Water pump turned ON");
} else if (distance >= UPPER_THRESHOLD) {
digitalWrite(RELAY_PIN, LOW); // Turn off the water pump
Serial.println("Water pump turned OFF");
}
} else {
Serial.println("Out of range");
}
// Bluetooth control
if (bluetooth.available()) {
char command = bluetooth.read();
if (command == '1') {
digitalWrite(RELAY_PIN, HIGH); // Turn on the water pump
Serial.println("Water pump turned ON via Bluetooth");
} else if (command == '0') {
digitalWrite(RELAY_PIN, LOW); // Turn off the water pump
Serial.println("Water pump turned OFF via Bluetooth");
}
}
}