-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoasis_auto_v2.ino
136 lines (110 loc) · 3.21 KB
/
oasis_auto_v2.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
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
// an auto irrigation system for indoor plants
//
// by mufasa159 (https://github.com/mufasa159)
#include <DHT.h>
#define PIN_BUTTON 2
#define PIN_DHT22 3
#define PIN_LED 4
#define PIN_PUMP 6
#define PIN_MOIST A1
DHT dht(PIN_DHT22, DHT22);
// ----------- User Settings Variables -------------
boolean displayFahrenheit = false;
enum Moisture {
VERY_WET = 150,
WET = 200,
NORMAL = 350,
DRY = 650,
VERY_DRY = 800
};
// -------------------------------------------------
float humidity;
float temperature;
float moisture;
const int DISCONNECTED = VERY_DRY; // Higher ? sensor possibly disconnected
Moisture MAX_MOIST = NORMAL; // Higher ? soil is dry
const int TOO_DRY = DRY; // Higher ? too dry to consider humidity
const float MIN_HUMIDITY = 40.00; // Lower ? lack of water on air
boolean pumpOn = false;
int currentPumpButtonValue = 0;
int previousPumpButtonValue = 1;
boolean pumpDisconnected = false;
long previousTime_01 = 0;
long previousTime_02 = 0;
long previousTime_03 = 0;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(PIN_PUMP, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_BUTTON, HIGH);
}
void loop() {
// Direct readings from sensors
moisture = analogRead(PIN_MOIST);
humidity = dht.readHumidity();
temperature = toFahrenheit(dht.readTemperature());
// Water pump power control via push-button
digitalWrite(PIN_PUMP, LOW);
currentPumpButtonValue = digitalRead(PIN_BUTTON);
if (currentPumpButtonValue == 0) {
previousPumpButtonValue = !previousPumpButtonValue;
if (previousPumpButtonValue == 0) {
pumpDisconnected = true;
} else {
pumpDisconnected = false;
}
while (currentPumpButtonValue == 0) {
currentPumpButtonValue = digitalRead(PIN_BUTTON);
}
}
// Timer for causing delay
unsigned long currentTime = millis();
// Pump LED Control
if (pumpDisconnected) {
digitalWrite(PIN_LED, LOW);
} else {
digitalWrite(PIN_LED, HIGH);
}
// Main logic for watering plants
if (currentTime - previousTime_03 >= 3000) {
if (!pumpDisconnected) {
if (moisture > DISCONNECTED) {
if (currentTime - previousTime_01 >= 3500) {
Serial.println("Moist Sensor Disonnected");
previousTime_01 = currentTime;
}
} else if (moisture > TOO_DRY) {
if (currentTime - previousTime_01 >= 3500) {
pumpOn = false;
previousTime_01 = currentTime;
} else {
pumpOn = true;
}
} else if (moisture > MAX_MOIST && humidity < MIN_HUMIDITY) {
if (currentTime - previousTime_01 >= 3500) {
pumpOn = false;
previousTime_01 = currentTime;
} else {
pumpOn = true;
}
}
}
Serial.print("Hmdt: ");
Serial.print(humidity);
Serial.print(" % Temp: ");
Serial.print(temperature);
Serial.print("ºF Mstr: ");
Serial.print(moisture);
Serial.print(" PumpDiscnt: ");
Serial.println(pumpDisconnected);
previousTime_03 = currentTime;
}
digitalWrite(PIN_PUMP, pumpOn);
}
// Convert Celsius temperature to Fahrenheit
float toFahrenheit(float celsiusValue) {
celsiusValue = (celsiusValue * 1.8) + 32;
return celsiusValue;
}