-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaterSystem.cpp
More file actions
142 lines (120 loc) · 3.17 KB
/
WaterSystem.cpp
File metadata and controls
142 lines (120 loc) · 3.17 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
#include <Arduino.h>
#include "WaterPump.cpp"
#include "Hygrometer.cpp"
#include "Config.cpp"
class WaterSystem {
private:
int unitid = UNITID;
int sigPin = SIG_PIN;
int tankPin = TANK_PIN;
char host[8];
float hum = 0;
float maxHum = HIGH_HUM_THRESH;
float minHum = LOW_HUM_THRESH;
WaterPump pump = WaterPump(PUMP_PIN);
Hygrometer hygro = Hygrometer(A0);
bool tankFilled = false;
bool sigOn = false;
bool justWatered = false;
int jWDelay = WATERING_DELAY;
int wDuration = WATERING_DURATION;
int jWTimer = 0;
public:
WaterSystem() {
snprintf(host, 6, "pump%d", UNITID);
pinMode(SIG_PIN, OUTPUT);
pinMode(TANK_PIN, INPUT);
}
const char* getHost() { return host; }
void pumpOn() {
pump.start();
justWatered = true;
jWTimer = 0;
}
void pumpOff() {
pump.stop();
justWatered = false;
jWTimer = 0;
}
void togglePump() {
if (pump.isRunning()) {
pumpOff();
} else {
pumpOn();
}
}
String getPumpStatus() {
if (pump.isRunning()) {
return "running";
} else {
return "stopped";
}
}
void setJWDelay(int delay) {
if (delay >= 0 && delay <= 3600)
jWDelay = delay;
}
int getJWDelay() {
return jWDelay;
}
void setWDuration(int duration) {
if (duration > 0 && duration < 60) {
wDuration = duration;
}
}
int getWDuration() {
return wDuration;
}
float getHum() {
return hum;
}
float getLowHumThresh() {
return minHum;
}
float getHighHumThresh() {
return maxHum;
}
void setHumThresh(const float lowThresh, const float highThresh) {
if (highThresh < 1 && highThresh > 0) {
maxHum = highThresh;
}
if (lowThresh < 1 && lowThresh > 0) {
minHum = lowThresh;
}
}
bool getTankFilled() {
return tankFilled;
}
void update() {
if (!justWatered) {
hum = hygro.measure();
tankFilled = digitalRead(tankPin);
if (!tankFilled) {
if (sigOn) {
digitalWrite(sigPin, LOW);
} else {
digitalWrite(sigPin, HIGH);
}
sigOn = !sigOn;
pump.stop();
return;
} else {
digitalWrite(sigPin, LOW);
}
if (hum > maxHum) {
pump.stop();
} else if (hum < minHum) {
pump.start();
justWatered = true;
}
} else {
jWTimer++;
if (jWTimer == wDuration * (1000 / MAIN_DELAY)) {
pump.stop();
} else if (jWTimer == (5 + jWDelay) * (1000 / MAIN_DELAY)) {
justWatered = false;
jWTimer = 0;
}
}
}
};