-
Notifications
You must be signed in to change notification settings - Fork 0
/
VMC-ATtiny85.ino
118 lines (106 loc) · 2.26 KB
/
VMC-ATtiny85.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
// VMC-ATtiny85 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, either version 3 of the License, or
// (at your option) any later version.
//
// VMC-ATtiny85 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 VMC-ATtiny85. If not, see <https://www.gnu.org/licenses/>.
#include <dht.h>
#define RELAY_PIN 1
#define LED_PIN 3
#define DHT11_PIN 4
#define REGULAR_HUMIDITY 55.0
#define RELAY_ON() digitalWrite(RELAY_PIN, LOW)
#define RELAY_OFF() digitalWrite(RELAY_PIN, HIGH)
#define LED_ON() digitalWrite(LED_PIN, HIGH)
#define LED_OFF() digitalWrite(LED_PIN, LOW)
dht DHT;
int latest_measure;
void flash_led(int measure)
{
LED_OFF();
delay(100);
for (int i = 0; i < measure / 10; i++) {
LED_ON();
delay(300);
LED_OFF();
delay(300);
}
}
int check_measure(int check)
{
static int err_count = 0;
if (check != DHTLIB_OK) {
err_count++;
if (err_count > 3) {
return 1;
}
return 0;
}
err_count = 0;
return 0;
}
int impossible_variation(int measure)
{
if (abs(measure - latest_measure) > 30)
return 1;
return 0;
}
void led_error_display(void)
{
LED_ON();
delay(50);
LED_OFF();
delay(50);
LED_ON();
delay(50);
LED_OFF();
}
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
LED_OFF();
RELAY_ON();
LED_ON();
delay(5000);
RELAY_OFF();
LED_OFF();
int chk = DHT.read11(DHT11_PIN);
if (check_measure(chk)) {
led_error_display();
RELAY_OFF();
return;
}
latest_measure = DHT.humidity;
}
void loop()
{
int chk;
int h = 0;
delay(2500);
chk = DHT.read11(DHT11_PIN);
if (check_measure(chk)) {
led_error_display();
RELAY_OFF();
return;
}
h = DHT.humidity;
if (impossible_variation(h)) {
return;
}
latest_measure = h;
flash_led(h - 5);
if (h < REGULAR_HUMIDITY) {
LED_OFF();
RELAY_OFF();
} else if (h > REGULAR_HUMIDITY + 5.0) {
LED_ON();
RELAY_ON();
}
}