-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerial.ino
667 lines (600 loc) · 19.7 KB
/
Serial.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* 25
Code for the (Un)original Prusa Drybox Heater
Written by Bram Elema, Creative Commons BY-NC-SA
Added support for a second temp/hum Sensor DHT21 or DHT22
and AutoOff Function
and AutoHum Function
and new Fan control, and Led Strip control
If heater is set on, the data now updated each 5 sec otherwise 10 sec
The TargetTemp is now 50°C, the AutoHum ist now 25%
Update Display if Heating on is now 2 sec, tempDiff is now 0.1°C/0.18°F
Werner Krenn - last modified 2024-12-11
REQUIRES the following Arduino libraries:
- Adafruit_GFX Library: https://github.com/adafruit/Adafruit-GFX-Library
- Adafruit_SSD1306 Library: https://github.com/adafruit/Adafruit_SSD1306
- DFRobot_DHT20 Sensor Library: https://github.com/DFRobot/DFRobot_DHT20
- DHT_sensor_library Library: https://github.com/adafruit/DHT-sensor-library
*/
#include "DHT.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DFRobot_DHT20.h>
#include <SPI.h>
#include <Wire.h>
// Uncomment the next line if you want the temperature to be displayed in Fahrenheit, it is displayed in Celcius by default
// #define Fahrenheit
// to see the Temp/Hum Data in the Serial Monitor uncomment next line
// #define debug
#define showFraction // all values with decimal values otherwise only integer values
#define Button1 6 // on/off
#define Button2 7 // + increase
#define Button3 8 // - decrease
#define Heater 10
#define Fan 5
#define Led 4
//#define PETG // Max = 73 °C 163
//#define ABS // Max = 83 °C 181
//#define ASA // Max = 93 °C 199
//#define PC // Max = 123 °C 253
//#define PA // Max = 143 °C 289
// If an LED strip needs to be controlled otherwise uncomment next line
#define controlLed
#define ControllLedAutooff // Led on and Temperature >=60°C/140°F (= max. operating temperature LED strip) the led is switched off
// if have not attached a second temp/hum Sensor (dht22 or dht21) uncomment next line
#define SecondTemp // should be placed on the floor under the heater or duct
#define DHT_PIN 9 // The Arduino Nano pin connected to DHT21/22 sensor
#define DHT_TYPE DHT21
//#define DHT_TYPE DHT22
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DFRobot_DHT20 dht20;
DHT dht(DHT_PIN, DHT_TYPE);
bool overHeat = true; // control overheating - only used if second temp/hum is available
bool withDuct = true; // Duct installed -> Tempdiff = 0.1°C otherwise 0.3°C
int TargetTemp = 50;
int Refresh = 2000; // Update values in msec if heater is on (former 5000)
int MaxSet = 70; // max allowed setting TargetTemp °C
int MinSet = 20; // min allowed setting TargetTemp °C
int AutoOffTime = 360; // here 6 hours
int MaxAutoOffTime = 2880; // max 2 days
int FanDelay = 300; // 300 seconds=5 minutes max 999 seconds because space Display
int FanCor = 50; // >= 50°C TargetTemp add to Fandelay 60 or 90 sec
int FanCor1 = 55; // all FanCor in °C - are later converted to Fahrenheit if set
int FanCor2 = 60;
//int FanCor3 = 65;
//int FanCor4 = 70;
int FanCor01 = 40;
int FanCor02 = 35;
int FanCor03 = 30;
int FanCor04 = 25;
int TargetHum = 25;
int AutoHumValue = 25;
int MaxHumSet = 65; // max allowed setting TargetHum
int MinHumSet = 10;
float humDiff = 0.1; // diff between hum values on/off
bool HumOff = false; // AutoHum trigger
int Max = 85; // overHeating Temp under the duct, need #overHeat = true and SecondTemp
int HeatMaxValue = 85;
int HeatMax = 140;
int HeatMin = 70;
float TemperatureCor = 0.0; // correction Temperatur
float HumidityCor = 0.0; // correction Humidity
float Temperature2Cor = 0.0; // correction Temperatur2 if #define SecondTemp
float Humidity2Cor = 0.0; // correction Humidity2 if #define SecondTemp
unsigned long previousMillis = 0;
unsigned long AutoOffpreviousMillis = 0;
unsigned long AutoOffSetpreviousMillis = 0;
unsigned long FanOnpreviousMillis = 0;
unsigned long FanHumOnpreviousMillis = 0;
unsigned long debugMillis = 0;
int FanValue = 0;
int AutoOffTimeValue;
bool status = false;
bool Hot = false;
bool AutoOff = false;
bool AutoOffSet = false;
bool AutoHum = false;
bool AutoHumSet = false;
bool HeatMaxSet = false;
bool FanOn = false;
bool FanHumOn = false;
bool TempHigh = false;
bool showSecondTemp = false;
bool showLed = false;
bool showLedSet = false;
char text[] = "°C";
float Temperature;
float Humidity;
float Temperature2;
float Humidity2;
float tempDiff;
char TempIntegerDisplay[4];
char TempFractionDisplay[4];
char HumIntegerDisplay[4];
char HumFractionDisplay[4];
char TargetTempDisplay[4];
char TargetHumDisplay[2];
char MinuteDisplay[4];
void setup(){
Serial.begin(9600);
//Serial.begin(115200);
// Initialize buttons and heater
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Heater, OUTPUT);
pinMode(Fan, OUTPUT);
pinMode(Led, OUTPUT);
#ifdef SecondTemp
dht.begin(); // initialize the sensor
showSecondTemp = true;
#endif
#ifdef controlLed
showLed = true;
//digitalWrite(Led, LOW);
#endif
// Check if sensor and display are working
if(dht20.begin()){
Serial.println("Initialize sensor failed");
delay(1000);
}
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("Initialize display failed");
delay(1000);
}
if (withDuct == true)
{
tempDiff = 0.1;
} else {
tempDiff = 0.2;
}
#ifndef Fahrenheit
int HeatMaxValue= Max;
#endif
#ifdef Fahrenheit
TargetTemp = int(TargetTemp *9/5) + 32);
MaxSet = int(MaxSet *9/5) + 32);
int Max = int(Max *9/5) + 32);
int HeatMax = int(HeatMax *9/5) + 32);
int HeatMin = int(HeatMin *9/5) + 32);
int HeatMaxValue= Max;
MinSet = int(MinSet *9/5) + 32);
int FanCor = int(FanCor *9/5) + 32);
int FanCor1 = int(FanCor1 *9/5) + 32);
int FanCor2 = int(FanCor2 *9/5) + 32);
//int FanCor3 = int(FanCor3 *9/5) + 32);
//int FanCor4 = int(FanCor4 *9/5) + 32);
int FanCor01 = int(FanCor01 *9/5) + 32);
int FanCor02 = int(FanCor02 *9/5) + 32);
int FanCor03 = int(FanCor03 *9/5) + 32);
int FanCor04 = int(FanCor04 *9/5) + 32);
char text[] = "°F";
if (withDuct == true)
{
tempDiff = 0.18;
} else {
tempDiff = 0.36;
}
#endif
AutoOffTimeValue = AutoOffTime;
FanValue = FanDelay;
drawLogo();
delay(2000);
sensorUpdate();
}
void loop(){
// Heating system ON:
while(status == true){
unsigned long currentMillis = millis();
FanOnpreviousMillis = currentMillis;
if (AutoOff == true) {
if(currentMillis - AutoOffpreviousMillis >= 60000) { // AutoOff Time reached (check every 1 Minutes)
AutoOffpreviousMillis = currentMillis;
AutoOffTimeValue = AutoOffTimeValue - 1;
if (AutoOffTimeValue <= 0){
status = false;
digitalWrite(Heater, LOW);
}
}
}
if (AutoHum == true) {
unsigned long currentMillisHum = millis();
if (Humidity <= AutoHumValue - humDiff)
{
digitalWrite(Heater, LOW);
HumOff = true;
Hot = false;
FanHumOn = true;
}
if ((Humidity >= AutoHumValue + humDiff))
{
if (TempHigh == false)
{
if(Temperature < (TargetTemp - tempDiff))
{
digitalWrite(Fan, HIGH);
//digitalWrite(Heater, HIGH);
Hot = true;
}
}
HumOff = false;
FanHumOn = false;
}
if (HumOff == false) {FanHumOnpreviousMillis = currentMillisHum; }
if (FanHumOn == true) {FanValue = FanDelay - ((currentMillisHum - FanHumOnpreviousMillis)/1000);}
if (( FanHumOn == true) and ((currentMillisHum - FanHumOnpreviousMillis)/1000 >= FanDelay)) { // Switch off Fan delay
FanHumOn = false;
digitalWrite(Fan, LOW);
}
}
if((digitalRead(Button2) == LOW) and (digitalRead(Button1) == LOW))
{
AutoOffSet = true;
AutoHum = false;
AutoHumSet = false;
AutoOffSetpreviousMillis = currentMillis;
AutoOff = true;
}
if((digitalRead(Button3) == LOW) and (digitalRead(Button1) == LOW))
{
AutoOffSet = false;
AutoHum = true;
AutoHumSet = true;
AutoOffSetpreviousMillis = currentMillis;
AutoOff = false;
}
#ifdef SecondTemp
if((digitalRead(Button3) == LOW) and (digitalRead(Button2) == LOW))
{
HeatMaxSet = true;
AutoOffSetpreviousMillis = currentMillis;
}
#endif
if ((AutoOffSet == true) and (currentMillis - AutoOffSetpreviousMillis >= 30000) ) // after 30 seconds settings for AutoOffSet off
{
AutoOffSet = false;
}
if ((AutoHumSet == true) and (currentMillis - AutoOffSetpreviousMillis >= 30000) ) // after 30 seconds settings for AutoHumSet off
{
AutoHumSet = false;
}
#ifdef SecondTemp
if ((HeatMaxSet == true) and (currentMillis - AutoOffSetpreviousMillis >= 30000) ) // after 30 seconds settings for AutoHumSet off
{
HeatMaxSet = false;
}
#ifdef controlLed
#ifdef ControllLedAutooff
#ifdef Fahrenheit
if ((showLed == true) and (Temperature2 >= 140)) {
showLedSet = true;
digitalWrite(Led, LOW);
}
#endif
#ifndef Fahrenheit
if ((showLed == true) and (Temperature2 >= 60)) {
showLedSet = true;
digitalWrite(Led, LOW);
}
#endif
#endif
#endif
#endif
if ((digitalRead(Button1) == LOW) and (digitalRead(Button2) == HIGH) and (digitalRead(Button3) == HIGH) ){ // Action button 1: switch heating system from on to off
status = !status;
AutoOff = false;
AutoHum = false;
AutoOffSet = false;
AutoHumSet = false;
AutoOffTimeValue = AutoOffTime;
if (FanOn == true)
{FanOnpreviousMillis = currentMillis;}
sensorUpdate();
digitalWrite(Heater, LOW);
Hot = false;
HeatMaxValue = Max;
display.clearDisplay();
drawStatus();
if (status == true) {
digitalWrite(Fan, HIGH);
FanOn = true;
}
if (not showSecondTemp){
drawTemperature();
drawHumidity();
}
else
{
drawTemperature1();
drawHumidity1();
drawTemperature2();
drawHumidity2();
}
display.display();
delay(200);
}
if(digitalRead(Button2) == LOW) { // Action button 2: increase target temperature
if ((AutoOffSet == false) and (AutoHumSet == false) and (HeatMaxSet == false)) {
TargetTemp = TargetTemp + 1;
if (TargetTemp > MaxSet)
{ TargetTemp = MaxSet;}
previousMillis = currentMillis;
display.clearDisplay();
drawTargetTemperature();
if (TargetTemp >= FanCor04) { FanDelay = 60; FanValue = FanDelay;}
if (TargetTemp >= FanCor03) { FanDelay = 120; FanValue = FanDelay;}
if (TargetTemp >= FanCor02) { FanDelay = 180; FanValue = FanDelay;}
if (TargetTemp >= FanCor01) { FanDelay = 240; FanValue = FanDelay;}
if (TargetTemp >= FanCor) { FanDelay = 300; FanValue = FanDelay;}
if (TargetTemp >= FanCor1) { FanDelay = 420; FanValue = FanDelay;}
if (TargetTemp >= FanCor2) { FanDelay = 570; FanValue = FanDelay;}
//if (TargetTemp >= FanCor3) { FanDelay = 780; FanValue = FanDelay;}
//if (TargetTemp >= FanCor4) { FanDelay = 900; FanValue = FanDelay;}
}
if ((AutoOffSet == true) and (AutoHumSet == false) and (HeatMaxSet == false)) // or increase AutoOff time
{
AutoOffTimeValue = AutoOffTimeValue + 10;
if (AutoOffTimeValue > MaxAutoOffTime)
{ AutoOffTimeValue = MaxAutoOffTime; }
previousMillis = currentMillis;
display.clearDisplay();
drawAutoOfftMinutes();
}
if ((AutoHumSet == true) and (AutoOffSet == false) and (HeatMaxSet == false))
{
AutoHumValue = AutoHumValue + 1;
if (AutoHumValue > MaxHumSet)
{ AutoHumValue = MaxHumSet;}
previousMillis = currentMillis;
display.clearDisplay();
drawAutoHum();
}
#ifdef SecondTemp
if ((AutoHumSet == false) and (AutoOffSet == false) and (HeatMaxSet == true))
{
HeatMaxValue = HeatMaxValue + 1;
if (HeatMaxValue > HeatMax)
{ HeatMaxValue = HeatMax;}
previousMillis = currentMillis;
display.clearDisplay();
drawHeatMax();
}
#endif
display.display();
delay(200);
}
if(digitalRead(Button3) == LOW) { // Action button 3: decrease target temperature
if ((AutoOffSet == false) and (AutoHumSet == false) and (HeatMaxSet == false)) {
TargetTemp = TargetTemp - 1;
if (TargetTemp < MinSet)
{ TargetTemp = MinSet;}
previousMillis = currentMillis;
display.clearDisplay();
drawTargetTemperature();
//if (TargetTemp < FanCor4) { FanDelay = 780; FanValue = FanDelay;}
//if (TargetTemp < FanCor3) { FanDelay = 660; FanValue = FanDelay;}
if (TargetTemp < FanCor2) { FanDelay = 420; FanValue = FanDelay;}
if (TargetTemp < FanCor1) { FanDelay = 300; FanValue = FanDelay;}
if (TargetTemp < FanCor) { FanDelay = 240; FanValue = FanDelay;}
if (TargetTemp < FanCor01) { FanDelay = 180; FanValue = FanDelay;}
if (TargetTemp < FanCor02) { FanDelay = 120; FanValue = FanDelay;}
if (TargetTemp < FanCor03) { FanDelay = 60; FanValue = FanDelay;}
if (TargetTemp < FanCor04) { FanDelay = 30; FanValue = FanDelay;}
}
if (AutoOffSet == true)
{
AutoOffTimeValue = AutoOffTimeValue - 10;
if (AutoOffTimeValue <= 0 )
{ AutoOffTimeValue = 10; }
previousMillis = currentMillis;
display.clearDisplay();
drawAutoOfftMinutes();
}
if ((AutoHumSet == true) and (AutoOffSet == false) and (HeatMaxSet == false))
{
AutoHumValue = AutoHumValue - 1;
if (AutoHumValue < MinHumSet)
{ AutoHumValue = MinHumSet;}
if (AutoHumValue < 25)
{
#ifdef Fahrenheit
if (TargetTemp < 131) {
TargetTemp = 131;
FanDelay = 420;
FanValue = FanDelay;
}
#endif
#ifndef Fahrenheit
if (TargetTemp < 55) {
TargetTemp = 55;
FanDelay = 420;
FanValue = FanDelay;
}
#endif
}
previousMillis = currentMillis;
display.clearDisplay();
drawAutoHum();
}
#ifdef SecondTemp
if ((AutoHumSet == false) and (AutoOffSet == false) and (HeatMaxSet == true))
{
HeatMaxValue = HeatMaxValue - 1;
if (HeatMaxValue < HeatMin)
{ HeatMaxValue = HeatMin;}
previousMillis = currentMillis;
display.clearDisplay();
drawHeatMax();
}
#endif
display.display();
delay(200);
}
if(currentMillis - previousMillis >= Refresh) { // Refreshes data on the display (every x mseconds)
previousMillis = currentMillis;
sensorUpdate();
display.clearDisplay();
drawStatus();
if (not showSecondTemp){
drawTemperature();
drawHumidity();
}
else
{
drawTemperature1();
drawHumidity1();
drawTemperature2();
drawHumidity2();
}
display.display();
heater();
}
if (overHeat == true)
{
if ((TempHigh == false) and (Hot == true))
{
digitalWrite(Heater, HIGH);
}
}
}
// Heating system OFF:
while(status == false){
unsigned long currentMillis = millis();
AutoOff = false;
AutoOffSet = false;
AutoOffTimeValue = AutoOffTime;
digitalWrite(Heater, LOW);
if (FanOn == true)
{
FanValue = FanDelay - ((currentMillis - FanOnpreviousMillis)/1000);
if((currentMillis - FanOnpreviousMillis)/1000 >= FanDelay) { // Switch off Fan delay
FanOn = false;
digitalWrite(Fan, LOW);
}
}
if(digitalRead(Button1) == LOW) { // Action button 1: switch heating system from off to on
status = !status;
sensorUpdate();
if (status == true) {
digitalWrite(Fan, HIGH);
FanOn = true;
}
display.clearDisplay();
drawStatus();
if (not showSecondTemp){
drawTemperature();
drawHumidity();
}
else
{
drawTemperature1();
drawHumidity1();
drawTemperature2();
drawHumidity2();
}
display.display();
delay(200);
}
#ifdef SecondTemp
if (status == false){
if((digitalRead(Button3) == LOW) and (not showSecondTemp)) { // Action button 3: second Temp view on
showSecondTemp = true;
display.clearDisplay();
drawStatus();
drawTemperature1();
drawHumidity1();
drawTemperature2();
drawHumidity2();
display.display();
delay(200);
}
if((digitalRead(Button3) == LOW) and (showSecondTemp)) { // Action button 3: second Temp view off
showSecondTemp = false;
display.clearDisplay();
drawStatus();
drawTemperature();
drawHumidity();
display.display();
delay(200);
}
}
#endif
#ifdef controlLed
if (status == false){
if((digitalRead(Button2) == LOW) and (not showLed)) { // Action button 2: LED on
showLed = true;
digitalWrite(Led, HIGH);
delay(200);
}
if((digitalRead(Button2) == LOW) and (showLed)) { // Action button 2: LED off
showLed = false;
showLedSet = false;
digitalWrite(Led, LOW);
delay(200);
}
}
#ifdef SecondTemp
#ifdef ControllLedAutooff
#ifdef Fahrenheit
if (((showLed == true) and (showLedSet == true)) and (Temperature2 < 140)) {
showLedSet = false;
digitalWrite(Led, HIGH);
} else {
if (Temperature2 < 140) {showLedSet == false;}
}
#endif
#ifndef Fahrenheit
if (((showLed == true) and (showLedSet == true)) and (Temperature2 < 60)) {
showLedSet = false;
digitalWrite(Led, HIGH);
} else {
if (Temperature2 < 60) {showLedSet == false;}
}
#endif
#endif
#endif
#endif
if(currentMillis - previousMillis >= 10000) { // Refreshes data on the display (every 10 seconds)
previousMillis = currentMillis;
sensorUpdate();
display.clearDisplay();
drawStatus();
if (not showSecondTemp){
drawTemperature();
drawHumidity();
}
else
{
drawTemperature1();
drawHumidity1();
drawTemperature2();
drawHumidity2();
}
display.display();
}
#ifdef debug
if(currentMillis - debugMillis >= 2000) { // Updates Serial Monitor data every 2 seconds
debugMillis = currentMillis;
Serial.print("DHT20: Humidity: ");
Serial.print(Humidity);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(Temperature);
Serial.println(text);
#ifdef SecondTemp
Serial.print("DHT21/22: Humidity: ");
Serial.print(Humidity2);
Serial.print("%");
Serial.print(" ");
Serial.print("Temperature: ");
Serial.print(Temperature2);
Serial.println(text);
#endif
}
#endif
}
}