-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcactus.ino
126 lines (99 loc) · 3.56 KB
/
cactus.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
/////////////////////////////////////////////////////////////////////////////////////////////////
// Cactus Tracker v1.0.9 / March 22, 2015
// by Maksym Ganenko <buratin.barabanus at Google Mail>
/////////////////////////////////////////////////////////////////////////////////////////////////
#include <dht.h>
const int PIN_LM35 = A4;
const int PIN_DHT22 = 13;
const int PIN_HEATER = 10;
const int PIN_LIGHT = 2;
const int DELAY_MS = 1000;
const int MAGIC = 10101;
const float TEMP_MAX = 40.0;
/////////////////////////////////////////////////////////////////////////////////////////////////
struct SmoothValue {
float value;
SmoothValue() : value(NAN) { }
bool valid() { return !isnan(value); }
void update(float aValue) { value = isnan(value) ? aValue : value * 0.95f + aValue * 0.05f; }
};
/////////////////////////////////////////////////////////////////////////////////////////////////
enum { OFF = 0, ON, AUTO };
int mode = AUTO;
bool heater = false;
bool light = false;
float heaterFrom = 5.f;
float heaterTo = 10.f;
SmoothValue tempLM35, tempDHT22, humidityDHT22;
dht dhtReader;
/////////////////////////////////////////////////////////////////////////////////////////////////
void startHeater() {
digitalWrite(PIN_HEATER, HIGH);
heater = true;
}
void stopHeater() {
digitalWrite(PIN_HEATER, LOW);
heater = false;
}
void startLight() {
digitalWrite(PIN_LIGHT, HIGH);
light = true;
}
void stopLight() {
digitalWrite(PIN_LIGHT, LOW);
light = false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
digitalWrite(PIN_HEATER, LOW);
pinMode(PIN_HEATER, OUTPUT);
digitalWrite(PIN_LIGHT, LOW);
pinMode(PIN_LIGHT, OUTPUT);
analogReference(INTERNAL);
for (int i = 0; i < 100; ++i) {
analogRead(PIN_LM35);
}
}
void loop() {
float temp = float(analogRead(PIN_LM35)) / 1024 * 1.1 / 10e-3;
tempLM35.update(temp);
int code = dhtReader.read22(PIN_DHT22);
if (code == DHTLIB_OK) {
tempDHT22.update(dhtReader.temperature);
humidityDHT22.update(dhtReader.humidity);
}
if (Serial.available()) {
if (Serial.parseInt() == MAGIC) {
int newMode = Serial.parseInt();
float newHeaterFrom = Serial.parseFloat();
float newHeaterTo = Serial.parseFloat();
int newLight = Serial.parseInt();
if (newMode >= OFF && newMode <= AUTO && newHeaterFrom < newHeaterTo) {
mode = newMode;
heaterFrom = newHeaterFrom;
heaterTo = newHeaterTo;
stopHeater();
}
if (newLight) startLight();
else stopLight();
}
}
bool overheat = tempLM35.value >= TEMP_MAX;
if (!overheat && (mode == ON || (mode == AUTO && tempLM35.value <= heaterFrom))) {
startHeater();
}
if (overheat || mode == OFF || (mode == AUTO && tempLM35.value >= heaterTo)) {
stopHeater();
}
Serial.print("mode = "); Serial.print(mode);
Serial.print(", tempLM35 = "); Serial.print(tempLM35.value);
Serial.print(", tempDHT22 = "); Serial.print(tempDHT22.value);
Serial.print(", humidityDHT22 = "); Serial.print(humidityDHT22.value);
Serial.print(", heater = "); Serial.print(heater);
Serial.print(", heaterFrom = "); Serial.print(heaterFrom);
Serial.print(", heaterTo = "); Serial.print(heaterTo);
Serial.print(", light = "); Serial.println(light);
delay(DELAY_MS);
}
/////////////////////////////////////////////////////////////////////////////////////////////////