-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomated-Water-Tank-Controller-for-Rental-Houses-Using-Arduino.ino
More file actions
222 lines (190 loc) · 7.04 KB
/
Automated-Water-Tank-Controller-for-Rental-Houses-Using-Arduino.ino
File metadata and controls
222 lines (190 loc) · 7.04 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <LiquidCrystal_I2C.h>
//16*2 LCD I2C Display Header file
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
// Pin Definitions
const int motorRelayPin = 13; // Motor relay output
const int valve1Pin = 8; // Valve 1 (solenoid for Tank 1)
const int valve2Pin = 9; // Valve 2 (solenoid for Tank 2)
const int valve3Pin = 10; // Valve 3 (solenoid for Tank 3)
const int switch1Pin = 5; // Switch 1 (User 1 for Tank 1)
const int switch2Pin = 6; // Switch 2 (User 2 for Tank 2)
const int switch3Pin = 7; // Switch 3 (User 3 for Tank 3)
const int floatSwitch1Pin = 2; // Float switch 1 (Tank 1 full)
const int floatSwitch2Pin = 3; // Float switch 2 (Tank 2 full)
const int floatSwitch3Pin = 4; // Float switch 3 (Tank 3 full)
// Queue to track order of requests (0 means no request)
int requestQueue[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Stores the order of requests (1 for User 1, 2 for User 2, 3 for User 3)
int currentRequestIndex = 0; // Tracks the current request being processed
int currentTank = 0; // Currently running task
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup() {
// Initialize serial communication
Serial.begin(9600);
Serial.println("System initialized");
// Initialize pins
pinMode(motorRelayPin, OUTPUT);
pinMode(valve1Pin, OUTPUT);
pinMode(valve2Pin, OUTPUT);
pinMode(valve3Pin, OUTPUT);
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);
pinMode(switch3Pin, INPUT_PULLUP);
pinMode(floatSwitch1Pin, INPUT_PULLUP);
pinMode(floatSwitch2Pin, INPUT_PULLUP);
pinMode(floatSwitch3Pin, INPUT_PULLUP);
// Ensure all outputs are OFF initially
digitalWrite(motorRelayPin, LOW);
digitalWrite(valve1Pin, LOW);
digitalWrite(valve2Pin, LOW);
digitalWrite(valve3Pin, LOW);
Serial.println("Motor and valves initialized to OFF state");
// Initialize the lcd
lcd.init();
// Turn on the Backlight
lcd.backlight();
}
void loop() {
// Read switch states (active low)
bool switch1Pressed = !digitalRead(switch1Pin);
bool switch2Pressed = !digitalRead(switch2Pin);
bool switch3Pressed = !digitalRead(switch3Pin);
// Read float switch states (active low)
bool floatSwitch1Active = !digitalRead(floatSwitch1Pin); // Tank 1 full
bool floatSwitch2Active = !digitalRead(floatSwitch2Pin); // Tank 2 full
bool floatSwitch3Active = !digitalRead(floatSwitch3Pin); // Tank 3 full
// Enqueue requests based on switch presses
if (switch1Pressed) {
enqueueRequest(1);
Serial.println("User 1 requested to fill Tank 1");
}
if (switch2Pressed) {
enqueueRequest(2);
Serial.println("User 2 requested to fill Tank 2");
}
if (switch3Pressed) {
enqueueRequest(3);
Serial.println("User 3 requested to fill Tank 3");
}
// Process the request queue
if (currentRequestIndex < 25 && requestQueue[currentRequestIndex] != 0) {
currentTank = requestQueue[currentRequestIndex];
if (currentTank == 1 && !floatSwitch1Active) {
startFilling(1); // Fill Tank 1
Serial.println("Filling Tank 1...");
}
if (currentTank == 1 && floatSwitch1Active) {
completeRequest(1); // Stop filling when Tank 1 is full
Serial.println("Tank 1 is full, stopping...");
}
if (currentTank == 2 && !floatSwitch2Active) {
startFilling(2); // Fill Tank 2
Serial.println("Filling Tank 2...");
}
if (currentTank == 2 && floatSwitch2Active) {
completeRequest(2); // Stop filling when Tank 2 is full
Serial.println("Tank 2 is full, stopping...");
}
if (currentTank == 3 && !floatSwitch3Active) {
startFilling(3); // Fill Tank 3
Serial.println("Filling Tank 3...");
}
if (currentTank == 3 && floatSwitch3Active) {
completeRequest(3); // Stop filling when Tank 3 is full
Serial.println("Tank 3 is full, stopping...");
}
}
showTextUsingLCD();
delay(100);
}
// Function to add a request to the queue
void enqueueRequest(int user) {
for (int i = 0; i < 25; i++) {
if (requestQueue[i] == 0) {
requestQueue[i] = user; // Add the user request to the next available slot
Serial.print("Request for Tank ");
Serial.print(user);
Serial.println(" added to queue");
break;
}
}
}
// Function to start filling a specific tank
void startFilling(int tank) {
digitalWrite(motorRelayPin, HIGH); // Turn on motor
Serial.println("Motor turned ON");
if (tank == 1) {
digitalWrite(valve1Pin, HIGH); // Open valve 1
digitalWrite(valve2Pin, LOW); // Ensure other valves are closed
digitalWrite(valve3Pin, LOW);
Serial.println("Valve 1 opened, others closed");
} else if (tank == 2) {
digitalWrite(valve2Pin, HIGH); // Open valve 2
digitalWrite(valve1Pin, LOW); // Ensure other valves are closed
digitalWrite(valve3Pin, LOW);
Serial.println("Valve 2 opened, others closed");
} else if (tank == 3) {
digitalWrite(valve3Pin, HIGH); // Open valve 3
digitalWrite(valve1Pin, LOW); // Ensure other valves are closed
digitalWrite(valve2Pin, LOW);
Serial.println("Valve 3 opened, others closed");
}
}
// Function to stop filling a specific tank and dequeue the request
void completeRequest(int tank) {
// Close the corresponding valve
if (tank == 1) digitalWrite(valve1Pin, LOW);
if (tank == 2) digitalWrite(valve2Pin, LOW);
if (tank == 3) digitalWrite(valve3Pin, LOW);
Serial.print("Valve ");
Serial.print(tank);
Serial.println(" closed");
// Move to the next request in the queue
currentRequestIndex++;
// If there are no more requests, turn off the motor
if (currentRequestIndex >= 25 || requestQueue[currentRequestIndex] == 0) {
digitalWrite(motorRelayPin, LOW); // Turn off motor
Serial.println("No more requests, motor turned OFF");
currentRequestIndex = 0; // Reset to start of queue
resetQueue(); // Clear the queue
}
}
// Function to reset the request queue
void resetQueue() {
for (int i = 0; i < 25; i++) {
requestQueue[i] = 0; // Clear all requests
}
Serial.println("Request queue reset");
}
void showTextUsingLCD() {
// Clear the display buffer
lcd.clear();
if(currentRequestIndex == 0 && requestQueue[0] == 0){
lcd.setCursor(2, 0);
lcd.print("Press SWITCH");
lcd.setCursor(4,1);
lcd.print("to START");
}else{
lcd.setCursor(0, 0);
lcd.print("Filling House ");
lcd.setCursor(14, 0);
lcd.print(currentTank);
lcd.setCursor(0,1);
lcd.print("Waiting :");
lcd.setCursor(9,1);
lcd.print(requestQueue[currentRequestIndex+1]);
lcd.setCursor(10,1);
lcd.print(",");
lcd.setCursor(11,1);
lcd.print(requestQueue[currentRequestIndex+2]);
lcd.setCursor(12,1);
lcd.print(",");
lcd.setCursor(13,1);
lcd.print(requestQueue[currentRequestIndex+3]);
lcd.setCursor(14,1);
lcd.print(",");
lcd.setCursor(15,1);
lcd.print(requestQueue[currentRequestIndex+4]);
}
}