-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempLogger.pde
238 lines (210 loc) · 5.12 KB
/
TempLogger.pde
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
#include <SdFat.h>
#include <SdFatUtil.h>
const long LOG_INTERVAL = 20 * 1000;
const int SD_PIN = 10;
const int LM35_PIN = 0;
const int ANALOG_PIN_RES = 1024;
const float INPUT_VOLTAGE = 5.0;
const float LM35_RES = 0.01;
const int DISP_B1 = 7;
const int DISP_C1 = 6;
const int DISP_A2 = 3;
const int DISP_B2 = 2;
const int DISP_C2 = A4;
const int DISP_D2 = A3;
const int DISP_E2 = A2;
const int DISP_F2 = 5;
const int DISP_G2 = 4;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
long start;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("Booting up...");
initDisp();
initSd();
findUniqueFilename();
start = millis();
}
void loop() {
float voltageReading = analogRead(LM35_PIN) * INPUT_VOLTAGE / ANALOG_PIN_RES;
float temp = voltageReading / LM35_RES;
dispData(temp);
transmitData(now(), temp);
delay(LOG_INTERVAL - (now() % LOG_INTERVAL));
}
void initDisp() {
pinMode(DISP_B1, OUTPUT);
pinMode(DISP_C1, OUTPUT);
pinMode(DISP_A2, OUTPUT);
pinMode(DISP_B2, OUTPUT);
pinMode(DISP_C2, OUTPUT);
pinMode(DISP_D2, OUTPUT);
pinMode(DISP_E2, OUTPUT);
pinMode(DISP_F2, OUTPUT);
pinMode(DISP_G2, OUTPUT);
clearDisp();
digitalWrite(DISP_G2, LOW);
}
void clearDisp() {
digitalWrite(DISP_B1, HIGH);
digitalWrite(DISP_C1, HIGH);
digitalWrite(DISP_A2, HIGH);
digitalWrite(DISP_B2, HIGH);
digitalWrite(DISP_C2, HIGH);
digitalWrite(DISP_D2, HIGH);
digitalWrite(DISP_E2, HIGH);
digitalWrite(DISP_F2, HIGH);
digitalWrite(DISP_G2, HIGH);
}
void initSd() {
pinMode(SD_PIN, OUTPUT);
bool success = false;
while (!success) {
success = card.init();
if (!success) {
error("initSd() -> Sd2Card.init()\nRetrying in two seconds...", false);
delay(2 * 1000);
}
}
if (!volume.init(card)) error("initSd() -> SdVolume.init(Sd2Card)", true);
if (!root.openRoot(volume)) error("initSd() -> SdFile.openRoot(SdVolume)", true);
}
void findUniqueFilename() {
char filename[] = "log000.txt";
for (int i = 0; i < 1000; ++i) {
filename[3] = i / 100 + '0';
filename[4] = i % 100 / 10 + '0';
filename[5] = i % 10 + '0';
if (file.open(root, filename, O_CREAT | O_EXCL | O_WRITE | O_APPEND)) {
Serial.println("Opening file: " + String(filename));
return;
}
}
error ("findUniqueFilename() -> SdFile.open(SdFile, char*, uint8_t)", true);
}
void dispData(float temp) {
int t = round(temp);
clearDisp();
if (t < 0) {
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_E2, LOW);
digitalWrite(DISP_F2, LOW);
return;
}
if (t > 19) {
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_E2, LOW);
digitalWrite(DISP_F2, LOW);
digitalWrite(DISP_G2, LOW);
return;
}
if (t > 9 && t < 20) {
digitalWrite(DISP_B1, LOW);
digitalWrite(DISP_C1, LOW);
t -= 10;
}
switch(t) {
case 0:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_E2, LOW);
digitalWrite(DISP_F2, LOW);
break;
case 1:
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
break;
case 2:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_E2, LOW);
digitalWrite(DISP_G2, LOW);
break;
case 3:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_G2, LOW);
break;
case 4:
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_F2, LOW);
digitalWrite(DISP_G2, LOW);
break;
case 5:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_F2, LOW);
digitalWrite(DISP_G2, LOW);
break;
case 6:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_E2, LOW);
digitalWrite(DISP_F2, LOW);
digitalWrite(DISP_G2, LOW);
break;
case 7:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
break;
case 8:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_D2, LOW);
digitalWrite(DISP_E2, LOW);
digitalWrite(DISP_F2, LOW);
digitalWrite(DISP_G2, LOW);
break;
case 9:
digitalWrite(DISP_A2, LOW);
digitalWrite(DISP_B2, LOW);
digitalWrite(DISP_C2, LOW);
digitalWrite(DISP_F2, LOW);
digitalWrite(DISP_G2, LOW);
break;
default:
digitalWrite(DISP_G2, LOW);
}
}
//void activateSegments(...) {
// va_list vl;
//
//}
void transmitData(long now, float temp) {
long s = now / 1000;
long m = s / 60;
long h = m / 60;
s %= 60;
m %= 60;
char data[30];
sprintf(data, "%.2ld:%.2ld:%.2ld, %d\n", h, m, s, round(temp));
transmit(data);
}
long now() {
return millis() - start;
}
void transmit(String s) {
Serial.print(s);
file.print(s);
if (!file.sync()) error("transmit(String) -> SdFile.sync()", false);
}
void error(String s, bool halt) {
Serial.println("ERROR: " + s);
if (halt)
while(1);
}