-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumidityI2CLCD.ino
144 lines (122 loc) · 2.6 KB
/
humidityI2CLCD.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
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define HUMIDITYWARNING 3
#define TEMPERATUREWARNING 4
#define HUM_LOW_TH 30
#define HUM_HIGH_TH 60
#define TEMP_LOW_TH 20
#define TEMP_HIGH_TH 30
#define DEBUG false
boolean humWarning = false;
boolean tempWarning = false;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(HUMIDITYWARNING, OUTPUT);
pinMode(TEMPERATUREWARNING, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(HUMIDITYWARNING, LOW);
digitalWrite(TEMPERATUREWARNING, LOW);
lcd.init();
lcd.backlight();
dht.begin();
debugSerial();
}
void debugSerial()
{
if (DEBUG)
{
Serial.begin(9600);
Serial.println("DHT22 test");
}
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
debugSensoryData(h, t);
if (isnan(t) || isnan(h))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Can't get reading");
lcd.setCursor(0, 1);
lcd.print("from DHT");
} else {
engageWarnings(h, t);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity ");
lcd.print(h, 1);
lcd.print("%");
if (humWarning)
{
lcd.setCursor(15, 0);
lcd.print("!");
}
lcd.setCursor(0, 1);
lcd.print("Temperat.");
lcd.print(t, 1);
lcd.print("C");
if (tempWarning)
{
lcd.setCursor(15, 1);
lcd.print("!");
}
}
delay(1000);
}
void engageWarnings(float h, float t)
{
if (!humWarning && (h < HUM_LOW_TH || h > HUM_HIGH_TH))
{
digitalWrite(HUMIDITYWARNING, HIGH);
humWarning = true;
}
if (humWarning && (h > HUM_LOW_TH && h < HUM_HIGH_TH))
{
digitalWrite(HUMIDITYWARNING, LOW);
humWarning = false;
}
if (!tempWarning && (t < TEMP_LOW_TH || t > TEMP_HIGH_TH))
{
digitalWrite(TEMPERATUREWARNING, HIGH);
tempWarning = true;
}
if (tempWarning && (t > TEMP_LOW_TH && t < TEMP_HIGH_TH))
{
digitalWrite(TEMPERATUREWARNING, LOW);
tempWarning = false;
}
debugWarningData();
}
void debugWarningData()
{
if (DEBUG)
{
if (humWarning)
Serial.println("Humidity alert is ON!");
if (tempWarning)
Serial.println("Temperature alert is ON!");
}
}
void debugSensoryData(float h, float t)
{
if (DEBUG)
{
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.println();
}
}