-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatering_system.ino
184 lines (150 loc) · 4.76 KB
/
watering_system.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
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
/*
*
* Automated watering system by Jonas Fredriksson
* Arduino controlling a water pump via a relay
* Input is taken from push and flip switches
* Status is shown by LEDs
*
*/
const int manualWateringButtonPin = 2; // DIGITAL PIN 2
const int waterFlowButtonPin = 3; // DIGITAL PIN 3
const int timeButtonPin = 4; // DIGITAL PIN 4
const int ledPinFlowLow = 5; // DIGITAL PIN 5
const int ledPinFlowMid = 6; // DIGITAL PIN 6
const int ledPinFlowHigh = 7; // DIGITAL PIN 7
const int ledPinTimeLow = 8; // DIGITAL PIN 8
const int ledPinTimeMid = 9; // DIGITAL PIN 9
const int ledPinTimeHigh = 10; // DIGITAL PIN 10
int allLeds[] = {ledPinFlowLow, ledPinFlowMid, ledPinFlowHigh,
ledPinTimeLow, ledPinTimeMid, ledPinTimeHigh};
const int autoModeButtonPin = 11; // DIGITAL PIN 11
const int resetButtonPin = 12; // DIGITAL PIN 12
const int statusButtonPin = 13; // DIGITAL PIN 13
const int relayPin = A0; // ANALOG PIN 0
const long SECOND_IN_MS = 1000;
const unsigned long MINUTE_IN_MS = 60 * SECOND_IN_MS;
const unsigned long HOUR_IN_MS = MINUTE_IN_MS * 60;
const int numberOfStates = 4;
const int numberOfLeds = 6;
const int statusDelay = 1500;
const int noFlow = 0;
const unsigned long lowFlow = 1 * MINUTE_IN_MS; // Set good values for low mid high
const unsigned long midFlow = 15 * MINUTE_IN_MS;
const unsigned long highFlow = 30 * MINUTE_IN_MS;
const long noInterval = 0;
const long lowInterval = 6 * HOUR_IN_MS;
const long midInterval = 12 * HOUR_IN_MS;
const long highInterval = 24 * HOUR_IN_MS;
bool manualWateringButtonState = false;
bool waterFlowButtonState = false;
bool timeButtonState = false;
bool autoMode = false;
bool resetButtonState = false;
int waterFlowState = 1; // Default value to handle interrupts when on vacation
unsigned long waterFlow[] = {noFlow, lowFlow, midFlow, highFlow};
int timeState = 1; // Default value to handle interrupts
unsigned long timeValues[] = {noInterval, lowInterval, midInterval,highInterval};
unsigned long previousMillis = millis(); //default value to avoid instant start
long interval = 0;
void setup() {
Serial.begin(9600);
pinMode(manualWateringButtonPin, INPUT_PULLUP);
pinMode(waterFlowButtonPin, INPUT_PULLUP);
pinMode(statusButtonPin, INPUT_PULLUP);
pinMode(timeButtonPin, INPUT_PULLUP);
pinMode(autoModeButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(ledPinFlowLow, OUTPUT);
pinMode(ledPinFlowMid, OUTPUT);
pinMode(ledPinFlowHigh, OUTPUT);
pinMode(ledPinTimeLow, OUTPUT);
pinMode(ledPinTimeMid, OUTPUT);
pinMode(ledPinTimeHigh, OUTPUT);
}
void loop() {
if (digitalRead(statusButtonPin) == LOW) {
showAllStatus();
}
// Handle water level
waterFlowButtonState = digitalRead(waterFlowButtonPin);
if (waterFlowButtonState == LOW) {
waterFlowState++;
if (waterFlowState >= numberOfStates) {
waterFlowState = 0;
}
showWaterFlowStatus();
}
// Handle time setting
timeButtonState = digitalRead(timeButtonPin);
if (timeButtonState == LOW) {
timeState++;
if (timeState >= numberOfStates) {
timeState = 0;
}
showTimeStatus();
}
autoMode = digitalRead(autoModeButtonPin);
unsigned long currentMillis = millis();
interval = timeValues[timeState];
// If interval has passed, do watering
if (timeState > 0 && autoMode == LOW &&
currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
water(waterFlow[waterFlowState]);
}
// If button is pressed do manual watering
manualWateringButtonState = digitalRead(manualWateringButtonPin);
if (manualWateringButtonState == LOW) {
Serial.print("Watering!");
water(waterFlow[waterFlowState]);
}
}
void water(unsigned long waterAmount) {
digitalWrite(relayPin, HIGH);
delay(waterAmount);
digitalWrite(relayPin, LOW);
}
void writeWaterFlowStateToLeds() {
if (waterFlowState > 0) {
digitalWrite(ledPinFlowLow, HIGH);
}
if (waterFlowState > 1) {
digitalWrite(ledPinFlowMid, HIGH);
}
if (waterFlowState > 2) {
digitalWrite(ledPinFlowHigh, HIGH);
}
}
void writeTimeStateToLeds() {
if (timeState > 0) {
digitalWrite(ledPinTimeLow, HIGH);
}
if (timeState > 1) {
digitalWrite(ledPinTimeMid, HIGH);
}
if (timeState > 2) {
digitalWrite(ledPinTimeHigh, HIGH);
}
}
void writeAllLedLow() {
for (int currentPin = 0; currentPin < numberOfLeds; currentPin++) {
digitalWrite(allLeds[currentPin], LOW);
}
}
void showAllStatus() {
writeWaterFlowStateToLeds();
writeTimeStateToLeds();
delay(statusDelay);
writeAllLedLow();
}
void showWaterFlowStatus() {
writeWaterFlowStateToLeds();
delay(statusDelay);
writeAllLedLow();
}
void showTimeStatus() {
writeTimeStateToLeds();
delay(statusDelay);
writeAllLedLow();
}