-
Notifications
You must be signed in to change notification settings - Fork 0
/
Irrigation.cpp
135 lines (108 loc) · 3.01 KB
/
Irrigation.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
#include "Irrigation.h"
#include "pins.h"
#define DEBUG 1
uint8_t SOIL_DEBOUNCE = 50;
Reservoir::Reservoir(uint8_t sensorPin) {
_sensorPin = sensorPin;
pinMode(_sensorPin, INPUT);
}
boolean Reservoir::isEmpty() {
return digitalRead(_sensorPin) == LOW;
}
// ============================================================
Pump::Pump(uint8_t pumpCtrlPin) {
_pumpCtrlPin = pumpCtrlPin;
_pumping = false;
pinMode(_pumpCtrlPin, OUTPUT);
}
boolean Pump::isPumping() {
return _pumping;
};
void Pump::start() {
digitalWrite(_pumpCtrlPin, HIGH);
_pumping = true;
}
void Pump::stop() {
digitalWrite(_pumpCtrlPin, LOW);
_pumping = false;
}
// ============================================================
Soil::Soil(uint8_t sensorPin, uint8_t calibrationPin, uint8_t sensorPwrPin, uint8_t calibrationPwrPin) {
_calibrationPin = calibrationPin;
_sensorPin = sensorPin;
_calibrationPwrPin = calibrationPwrPin;
_sensorPwrPin = sensorPwrPin;
_sensorReading = 0;
_calibrationReading = 0;
pinMode(_sensorPwrPin, OUTPUT);
pinMode(_calibrationPwrPin, OUTPUT);
}
void Soil::_takeReading() {
digitalWrite(_sensorPwrPin, HIGH);
digitalWrite(_calibrationPwrPin, HIGH);
_sensorReading = analogRead(_sensorPin);
_calibrationReading = analogRead(_calibrationPin);
digitalWrite(_sensorPwrPin, LOW);
digitalWrite(_calibrationPwrPin, LOW);
}
boolean Soil::isTooDry() {
_takeReading();
#ifdef DEBUG
Serial.print("Calibration: ");
Serial.println(_calibrationReading);
Serial.print("Sensor: ");
Serial.println(_sensorReading);
#endif
return _sensorReading < _calibrationReading;
}
boolean Soil::isWetEnough() {
_takeReading();
#ifdef DEBUG
Serial.print("Calibration + Debounce: ");
Serial.println(_calibrationReading + SOIL_DEBOUNCE);
Serial.print("Sensor: ");
Serial.println(_sensorReading);
#endif
return _sensorReading > (_calibrationReading + SOIL_DEBOUNCE);
}
// ============================================================
Irrigation::Irrigation() :
reservoir(RES_SENSOR_PIN),
pump(PUMP_CTRL_PIN),
soil(MOISTURE_SENSOR_PIN, MOISTURE_CALIB_SENSOR_PIN, MOISTURE_PWR_PIN, MOISTURE_CALIB_PWR_PIN) {
_indicatorPin = POLL_LED_PIN;
_waterWarningPin = WATER_WRN_LED_PIN;
}
void Irrigation::init() {
pinMode(_indicatorPin, OUTPUT);
pinMode(_waterWarningPin, OUTPUT);
#ifdef DEBUG
Serial.begin(9600);
#endif
}
void Irrigation::pollIrrigationStatus() {
boolean reservoirEmpty = reservoir.isEmpty();
boolean soilTooDry = soil.isTooDry();
boolean soilWetEnough = soil.isWetEnough();
boolean pumping = pump.isPumping();
digitalWrite(_indicatorPin, HIGH);
if(reservoirEmpty) {
#ifdef DEBUG
Serial.println("The water reservoir is empty");
#endif
digitalWrite(_waterWarningPin, HIGH);
} else {
digitalWrite(_waterWarningPin, LOW);
}
if(pumping) {
if(reservoirEmpty || soilWetEnough) {
pump.stop();
}
} else {
if(!reservoirEmpty && soilTooDry) {
pump.start();
}
}
delay(10);
digitalWrite(_indicatorPin, LOW);
}