-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-climate-controller.ino
More file actions
212 lines (184 loc) · 4.97 KB
/
micro-climate-controller.ino
File metadata and controls
212 lines (184 loc) · 4.97 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <LiquidCrystal.h>
#include "src/DHT/DHT.h"
#define DHTPIN 13
#define DHTTYPE DHT11
#define HUMIDIFIERPIN 8
#define HEATERPIN 9
#define AVGCOUNT 5
#define TEMPSETPIN A1
#define HUMIDITYSETPIN A0
#define MINTEMP 68
#define MAXTEMP 80
#define MINHUMIDITY 30
#define MAXHUMIDITY 40
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
DHT dht(DHTPIN, DHTTYPE);
byte upArrow[8] = {
B00000,
B00100,
B01110,
B10101,
B00100,
B00100,
B00100,
B00000
};
uint8_t targetHumidity = 0;
uint8_t targetTemp = 0;
uint8_t humidifierOn = 0;
uint8_t heaterOn = 0;
uint8_t frame = 0;
float rollingHumidity[AVGCOUNT];
float rollingTemp[AVGCOUNT];
uint8_t prevSetTempAnalog = 0;
uint8_t setTempAnalog = 0;
uint8_t prevSetHumidityAnalog = 0;
uint8_t setHumidityAnalog = 0;
unsigned long currMillis = 0;
unsigned long prevMillis = 0;
long readInterval = 2000;
void printClimate(float humidity, float temp);
float rollingAvg(float arr[]);
uint8_t changeTarget(char type);
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, upArrow);
dht.begin();
memset(rollingHumidity, 0, sizeof rollingHumidity);
memset(rollingTemp, 0, sizeof rollingTemp);
pinMode(HUMIDIFIERPIN, OUTPUT);
pinMode(HEATERPIN, OUTPUT);
prevSetTempAnalog = map(analogRead(TEMPSETPIN), 1015, 15, MINTEMP, MAXTEMP);
prevSetHumidityAnalog = map(analogRead(HUMIDITYSETPIN), 1015, 15, MINHUMIDITY, MAXHUMIDITY);
targetHumidity = prevSetHumidityAnalog;
targetTemp = prevSetTempAnalog;
lcd.setCursor(0, 0);
lcd.print("Initializing....");
lcd.setCursor(0, 1);
lcd.print(" 0 % ");
delay(500);
do {
currMillis = millis();
if (currMillis - prevMillis >= readInterval) {
prevMillis = currMillis;
rollingHumidity[frame] = dht.readHumidity();
rollingTemp[frame] = dht.readTemperature(true);
frame++;
}
if (frame < AVGCOUNT) {
lcd.setCursor(6, 1);
} else {
lcd.setCursor(5, 1);
}
lcd.print((int) ((frame) * (10 / AVGCOUNT)));
} while (frame < AVGCOUNT);
frame = 0;
delay(500);
lcd.clear();
}
void loop() {
currMillis = millis();
if (currMillis - prevMillis >= readInterval) {
prevMillis = currMillis;
rollingHumidity[frame] = dht.readHumidity();
rollingTemp[frame] = dht.readTemperature(true);
frame++;
frame %= AVGCOUNT;
}
float h = rollingAvg(rollingHumidity);
float t = rollingAvg(rollingTemp);
printClimate(h, t);
if (round(h) < targetHumidity && !humidifierOn) {
digitalWrite(HUMIDIFIERPIN, HIGH);
humidifierOn = 1;
} else if (round(h) > targetHumidity && humidifierOn) {
digitalWrite(HUMIDIFIERPIN, LOW);
humidifierOn = 0;
}
if (round(t) < targetTemp && !heaterOn) {
digitalWrite(HEATERPIN, HIGH);
heaterOn = 1;
} else if (round(t) > targetTemp && heaterOn) {
digitalWrite(HEATERPIN, LOW);
heaterOn = 0;
}
setTempAnalog = map(analogRead(TEMPSETPIN), 1015, 15, MINTEMP, MAXTEMP);
setHumidityAnalog = map(analogRead(HUMIDITYSETPIN), 1015, 15, MINHUMIDITY, MAXHUMIDITY);
if (prevSetTempAnalog != setTempAnalog) {
targetTemp = changeTarget('t');
setTempAnalog = map(analogRead(TEMPSETPIN), 1015, 15, MINTEMP, MAXTEMP);
}
if (prevSetHumidityAnalog != setHumidityAnalog) {
targetHumidity = changeTarget('h');
setHumidityAnalog = map(analogRead(HUMIDITYSETPIN), 1015, 15, MINHUMIDITY, MAXHUMIDITY);
}
prevSetTempAnalog = setTempAnalog;
prevSetHumidityAnalog = setHumidityAnalog;
}
void printClimate(float humidity, float temp) {
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.setCursor(6, 0);
lcd.print(temp);
lcd.setCursor(14, 0);
lcd.print(char(223));
lcd.print("F");
lcd.setCursor(10, 1);
lcd.print(humidity);
lcd.setCursor(15, 1);
lcd.print("%");
if (heaterOn) {
lcd.setCursor(5, 0);
lcd.write(byte(0));
} else {
lcd.setCursor(5, 0);
lcd.print(" ");
}
if (humidifierOn) {
lcd.setCursor(9, 1);
lcd.write(byte(0));
} else {
lcd.setCursor(9, 1);
lcd.print(" ");
}
return;
}
float rollingAvg(float arr[]) {
float avg = 0;
for (int i = 0; i < AVGCOUNT; i++) {
avg += arr[i];
}
return avg/AVGCOUNT;
}
uint8_t changeTarget(char type) {
if (type == 'h') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Humidity:");
lcd.setCursor(3, 1);
lcd.print("%");
for (int i = 0; i < 40; i++) {
lcd.setCursor(0, 1);
lcd.print(map(analogRead(HUMIDITYSETPIN), 1015, 15, MINHUMIDITY, MAXHUMIDITY));
delay(200);
}
lcd.clear();
return map(analogRead(HUMIDITYSETPIN), 1015, 15, MINHUMIDITY, MAXHUMIDITY);
} else if (type == 't') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Temp:");
lcd.setCursor(3, 1);
lcd.print(char(223));
lcd.print("F");
for (int i = 0; i < 40; i++) {
lcd.setCursor(0, 1);
lcd.print(map(analogRead(TEMPSETPIN), 1015, 15, MINTEMP, MAXTEMP));
delay(200);
}
lcd.clear();
return map(analogRead(TEMPSETPIN), 1015, 15, MINTEMP, MAXTEMP);
}
}