-
Notifications
You must be signed in to change notification settings - Fork 0
/
bioreactor.ino
155 lines (137 loc) · 3.51 KB
/
bioreactor.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
/*
* This file is part of the home composting bioreactor project on
* https://github.com/lfagundes/bioreactor
*
* Copyright (c) 2024 Luis Fagundes
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "log.h"
#include "wiring.h"
#include "weather.h"
void setup() {
Serial.begin(9600);
resetClock();
delay(1000);
// Initialize all valve pins as OUTPUTs
for (int i = 0; i < numValves; i++) {
pinMode(valvePins[i], OUTPUT);
// Ensure all irrigation outputs start OFF
digitalWrite(valvePins[i], LOW);
}
pinMode(waterLevelSensorPin, INPUT_PULLUP);
pinMode(lightSensorPin, INPUT);
pinMode(waterInputValvePin, OUTPUT);
digitalWrite(waterInputValvePin, LOW);
pinMode(circulationValvePin, OUTPUT);
digitalWrite(circulationValvePin, LOW);
pinMode(waterPumpPin, OUTPUT);
digitalWrite(waterPumpPin, LOW);
pinMode(airPumpPin, OUTPUT);
digitalWrite(airPumpPin, LOW);
logMessage("Bioreactor initialized");
}
void pumpOn() {
digitalWrite(waterPumpPin, HIGH);
logMessage("Water pump ON");
}
void pumpOff() {
digitalWrite(waterPumpPin, LOW);
logMessage("Water pump OFF");
}
void openCirculation() {
digitalWrite(circulationValvePin, HIGH);
delay(500);
}
void closeCirculation() {
delay(500);
digitalWrite(circulationValvePin, LOW);
}
void open(int valves) {
setValves(valves, HIGH);
delay(500);
}
void close(int valves) {
delay(500);
setValves(valves, LOW);
}
void setValves(int valves, int state) {
for (int i = 0; i < numValves; i++) {
if (valves & (1 << i)) {
digitalWrite(valvePins[i], state);
String message = "Valve " + String(i) + (state == HIGH ?
" opened" : " closed");
logMessage(message.c_str());
}
}
}
bool waterAboveThreshold() {
bool connected = digitalRead(waterLevelSensorPin) == LOW; // LOW means connected
if (connected) {
logMessage("Water level is low, needs water");
} else {
logMessage("Water level is OK");
}
return !connected;
}
void inputWater(int t) {
for (int i=0; i < t; i++) {
if (waterAboveThreshold()) {
digitalWrite(waterInputValvePin, LOW);
return;
}
digitalWrite(waterInputValvePin, HIGH);
delay(1000);
}
digitalWrite(waterInputValvePin, LOW);
}
void cleanUp() {
openCirculation();
pumpOn();
delay(5000);
pumpOff();
closeCirculation();
}
void loop() {
while (true) {
waitForDay();
resetClock();
inputWater(120);
while (!isNight()) {
// Cleanup
openCirculation();
pumpOn();
delay(5000);
// Collect nutrients for 10s
open(valveH);
delay(10000);
// Irrigate cannabis for 1 minute
open(valveG);
close(valveH);
delay(60000);
pumpOff();
close(valveG);
// Now irrigate nutrients every 10 minutes for 4 hours
for (int i=0; i<24; i++) {
delay(600000);
open(valveH);
pumpOn();
delay(10000);
pumpOff();
close(valveH);
inputWater(30);
}
}
}
}