-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5SecWithoutFirebase.cpp
140 lines (111 loc) · 3.11 KB
/
5SecWithoutFirebase.cpp
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
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <FirebaseESP32.h>
#define SENSOR1 26
#define SENSOR2 27
long currentMillis = 0;
long previousMillis = 0;
int interval = 5000;
float calibrationFactor = 1.32;
volatile byte pulseCount1;
byte pulse1Sec1 = 0;
float flowRate1;
unsigned int flowMilliLitres1;
unsigned long totalMilliLitres1;
volatile byte pulseCount2;
byte pulse1Sec2 = 0;
float flowRate2;
unsigned int flowMilliLitres2;
unsigned long totalMilliLitres2;
boolean leakDetected = false;
unsigned long leakDetectedTime = 0;
boolean recheckLeak = false;
unsigned long recheckTime = 0;
void IRAM_ATTR pulseCounter1()
{
pulseCount1++;
}
void IRAM_ATTR pulseCounter2()
{
pulseCount2++;
}
void setup()
{
Serial.begin(115200);
pinMode(SENSOR1, INPUT_PULLUP);
pinMode(SENSOR2, INPUT_PULLUP);
pulseCount1 = 0;
flowRate1 = 0.0;
flowMilliLitres1 = 0;
totalMilliLitres1 = 0;
pulseCount2 = 0;
flowRate2 = 0.0;
flowMilliLitres2 = 0;
totalMilliLitres2 = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR1), pulseCounter1, FALLING);
attachInterrupt(digitalPinToInterrupt(SENSOR2), pulseCounter2, FALLING);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
pulse1Sec1 = pulseCount1;
pulse1Sec2 = pulseCount2;
pulseCount1 = 0;
pulseCount2 = 0;
flowRate1 = ((1000.0 / (currentMillis - previousMillis)) * pulse1Sec1) / calibrationFactor;
flowRate2 = ((1000.0 / (currentMillis - previousMillis)) * pulse1Sec2) / calibrationFactor;
previousMillis = millis();
flowMilliLitres1 = (flowRate1 / 60) * 1000;
flowMilliLitres2 = (flowRate2 / 60) * 1000;
totalMilliLitres1 += flowMilliLitres1;
totalMilliLitres2 += flowMilliLitres2;
Serial.print("Flow rate 1: ");
Serial.print(int(flowRate1));
Serial.print("L/min");
Serial.print("\t");
Serial.print("Output Liquid Quantity 1: ");
Serial.print(totalMilliLitres1);
Serial.print("mL / ");
Serial.print(totalMilliLitres1 / 1000);
Serial.println("L");
Serial.print("Flow rate 2: ");
Serial.print(int(flowRate1));
Serial.print("L/min");
Serial.print("\t");
Serial.print("Output Liquid Quantity 2: ");
Serial.print(totalMilliLitres2);
Serial.print("mL / ");
Serial.print(totalMilliLitres2 / 1000);
Serial.println("L\n");
if (flowRate1 > (flowRate2 + (flowRate2 * 0.1))) {
if (!leakDetected) {
leakDetectedTime = currentMillis;
recheckLeak = true;
recheckTime = currentMillis + 5000;
Serial.println("Leak Detected\n");
}
else
{
if (currentMillis - leakDetectedTime >= 5000) {
recheckLeak = false;
Serial.println("!!!!!!!!!!!! Leak Confirmed !!!!!!!!!!!!\n");
} else if (recheckLeak && currentMillis >= recheckTime) {
if (flowRate1 < (flowRate2 + (flowRate2 * 0.1))) {
recheckLeak = false;
}
Serial.println("Leak Resolved\n");
}
}
leakDetected = true;
} else {
if (leakDetected) {
leakDetected = false;
recheckLeak = false;
}
Serial.println("Leak Resolved\n");
}
}
}