forked from mtu-most/colorimeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorimeter.ino
379 lines (349 loc) · 8.34 KB
/
colorimeter.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
// vim: set foldmethod=marker filetype=cpp:
// Pin layout of the colorimeter: {{{
// D10 Enable for main sensor (should always be on). (yellow)
#define SENSOR_MAIN 10
// D11 Power for sensors (should always be high). (brown)
// Power for LEDs (should always be high). (red)
#define POWER 11
// D2 Enable for orange sensor. (purple)
#define SENSOR_620 2
// D3 Enable for IR sensor. (green)
#define SENSOR_IR 3
// D9 Enable for orange LED. (grey)
#define LED_620 9
// D8 Enable for IR LED. (blue)
#define LED_IR 8
// D4 Count from main sensor. (black)
#define COUNT_MAIN 4
// D5 Count from reference sensors (only one must be enabled). (orange)
#define COUNT_REF 5
// A4 + A5: I2C pins; LCD shield connected to it.
// Enable pins are active by setting them to output low; inactive by setting
// them to input. So all are set to low and toggling is done by changing
// the pin direction.
// }}}
enum SourceNames { // {{{
SOURCE_NONE,
SOURCE_IR,
SOURCE_620
}; // }}}
// Includes. {{{
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <LcdMenu.h>
#include <DuplexFrequencyCounter.h>
// }}}
// Globals. {{{
#define WARMUP_TIME 1000 // For testing, 1 second warmup time will do.
static uint16_t measure_time = 1000;
Adafruit_RGBLCDShield lcd;
static uint32_t newsource = 0;
static uint32_t cNTU = 4000; // centi-nephalometric turbidity units.
static float cod_zero_value = ~0;
static uint32_t sensor, ref; // Make these global for debugging.
// Nephalometry calibration values.
struct Neph_cal { // {{{
int num;
float x;
float y;
float xy;
float x2;
float y2;
float a;
float b;
}; // }}}
// The thing below is in eeprom, so the bytes must be directly addressable.
typedef struct
{
float cod;
Neph_cal neph;
} cal_t;
static union {
cal_t c;
char bytes[sizeof (cal_t)];
} cal;
// }}}
void writeCal () // {{{
{
for (uint8_t i = 0; i < sizeof (cal); ++i)
EEPROM.write (i, cal.bytes[i]);
} // }}}
void setSource (uint8_t source) // {{{
{
pinMode (SENSOR_IR, INPUT);
pinMode (SENSOR_620, INPUT);
pinMode (LED_IR, INPUT);
pinMode (LED_620, INPUT);
switch (source)
{
case SOURCE_NONE:
break;
case SOURCE_IR:
pinMode (SENSOR_IR, OUTPUT);
pinMode (LED_IR, OUTPUT);
break;
case SOURCE_620:
pinMode (SENSOR_620, OUTPUT);
pinMode (LED_620, OUTPUT);
break;
}
newsource = millis ();
} // }}}
void waitForSource () // {{{
{
uint32_t wait = millis () - newsource;
if (wait < WARMUP_TIME)
{
lcd.clear ();
message ("Source warms up", "Please wait");
delay (WARMUP_TIME - wait);
}
} // }}}
float getSensor () // {{{
{
readFreq (measure_time, sensor, ref);
return (float)sensor / ref;
} // }}}
// Menus, including implementation of the functions. {{{
MenuItem (COD_zero) // {{{
{
waitForSource ();
cod_zero_value = getSensor ();
return 1;
} // }}}
MenuItem (COD_measure) // {{{
{
static uint32_t index = 0;
// Don't measure without zero.
if (cod_zero_value == ~0)
{
message ("Please zero the", "instrument first");
waitForButton ();
return -1;
}
waitForSource ();
message ("Measuring");
float data = getSensor ();
// TODO: compute concentration.
float trans = data / cod_zero_value;
float absorb = -log10 (trans);
lcd.clear ();
lcd.print ("T:");
lcd.print (trans, 3);
lcd.setCursor (8, 0);
lcd.print ("A:");
lcd.print (absorb, 3);
lcd.setCursor (0, 1);
lcd.print (index);
lcd.print (" ");
unsigned long m = millis ();
lcd.print (m);
Serial.print (absorb, 3);
Serial.print ("\t");
Serial.print (index++);
Serial.print ("\t");
Serial.print (m);
Serial.print ("\n");
waitForButton ();
return 0;
} // }}}
MenuItem (COD_calibrate) // {{{
{
waitForSource ();
return -1;
} // }}}
Menu <3> COD_Menu ("COD", (char const *[]){"Zero", "Measure", "Calibrate"}, (action *[]){&COD_zero, &COD_measure, &COD_calibrate});
MenuItem (doCOD) // {{{
{
setSource (SOURCE_620);
bool ret = COD_Menu.run ();
setSource (SOURCE_NONE);
return ret;
} // }}}
MenuItem (Neph_measure) // {{{
{
static uint32_t index = 0;
waitForSource ();
message ("Measuring");
float data = getSensor ();
if (isnan (cal.c.neph.a))
{
lcd.clear ();
lcd.print ("Not calibrated");
lcd.setCursor (0, 1);
lcd.print ("Read: ");
int power = 0;
while (data < 1) {
power -= 1;
data *= 10;
}
while (data >= 10) {
power += 1;
data /= 10;
}
lcd.print (data);
lcd.print ("e");
lcd.print (power);
}
else
{
float turb = cal.c.neph.a * data + cal.c.neph.b;
lcd.clear ();
lcd.print (turb, 3);
lcd.print (" NTU");
lcd.setCursor (0, 1);
lcd.print ("index: ");
lcd.print (index);
Serial.print (index++);
Serial.print ("\t");
Serial.print (turb);
Serial.print ("\t");
Serial.print (millis ());
Serial.print ("\n");
}
waitForButton ();
return 0;
} // }}}
MenuItem (Neph_recalibrate) // {{{
{
cal.c.neph.a = NAN;
cal.c.neph.b = NAN;
cal.c.neph.num = 0;
cal.c.neph.x = 0;
cal.c.neph.y = 0;
cal.c.neph.xy = 0;
cal.c.neph.x2 = 0;
cal.c.neph.y2 = 0;
writeCal ();
message ("Calibration", "erased.");
waitForButton ();
return 1;
} // }}}
MenuItem (Neph_calibrate) // {{{
{
message ("Calibration:", "0000.00 NTU ");
cNTU = readNum (0, 1, 4, 2, cNTU);
waitForSource ();
message ("Calibrating");
float data = getSensor ();
cal.c.neph.x += data;
cal.c.neph.y += cNTU / 100.;
cal.c.neph.xy += data * (cNTU / 100.);
cal.c.neph.x2 += data * data;
cal.c.neph.y2 += (cNTU / 100.) * (cNTU / 100.);
++cal.c.neph.num;
if (cal.c.neph.num >= 4 && cal.c.neph.y2 != cal.c.neph.y * cal.c.neph.y)
{
cal.c.neph.a = (cal.c.neph.xy - cal.c.neph.x * cal.c.neph.y / cal.c.neph.num) / (cal.c.neph.x2 - cal.c.neph.x * cal.c.neph.x / cal.c.neph.num);
cal.c.neph.b = (cal.c.neph.y - cal.c.neph.a * cal.c.neph.x) / cal.c.neph.num;
writeCal ();
message ("Calibration read");
lcd.setCursor (0, 1);
lcd.print ("Error: ");
lcd.print ((cal.c.neph.y2 + cal.c.neph.a * cal.c.neph.a * cal.c.neph.x2 - 2 * cal.c.neph.a * cal.c.neph.xy - 2 * cal.c.neph.b * cal.c.neph.y + 2 * cal.c.neph.a * cal.c.neph.b + cal.c.neph.x) / cal.c.neph.num + cal.c.neph.b * cal.c.neph.b);
}
else
message ("Calibration read", "More needed");
waitForButton ();
return 0;
} // }}}
Menu <3> Neph_Menu ("Nephalometry", (char const *[]){"Measure", "Recalibrate", "Calibrate"}, (action *[]){&Neph_measure, &Neph_recalibrate, &Neph_calibrate});
MenuItem (doNeph) // {{{
{
setSource (SOURCE_IR);
bool ret = Neph_Menu.run ();
setSource (SOURCE_NONE);
return ret;
} // }}}
void debug_newsource (uint8_t s) // {{{
{
setSource (s);
lcd.setCursor (0, 1);
lcd.print ("Source: ");
switch (s)
{
case SOURCE_NONE:
lcd.print ("None ");
break;
case SOURCE_620:
lcd.print ("620 ");
break;
case SOURCE_IR:
lcd.print ("IR");
break;
}
} // }}}
MenuItem (debug) // {{{
{
measure_time = 100;
lcd.print ("Time: 0100 ms");
uint8_t s = SOURCE_NONE;
while (lcd.readButtons () != 0) {}
debug_newsource (s);
while (true)
{
uint8_t b = lcd.readButtons ();
switch (b)
{
case 0:
break;
case BUTTON_UP:
s = (s + 1) % 3;
debug_newsource (s);
while (lcd.readButtons () != 0) {}
break;
case BUTTON_DOWN:
s = (s - 1 + 3) % 3;
debug_newsource (s);;
while (lcd.readButtons () != 0) {}
break;
case BUTTON_LEFT:
setSource (SOURCE_NONE);
return 0;
case BUTTON_RIGHT:
case BUTTON_SELECT:
measure_time = readNum (6, 0, 4, 0, measure_time);
while (lcd.readButtons () != 0) {}
}
// Let the serial port catch up.
while (~UCSR0A & (1 << 5)) {}
// Send data to the port.
unsigned long m = millis ();
float data = getSensor ();
Serial.print (m);
Serial.print ("\t");
Serial.print (data * 1e6);
Serial.print ("\t");
Serial.print (sensor);
Serial.print ("\t");
Serial.print (ref);
Serial.print ("\t");
Serial.print (s);
Serial.print ("\t");
Serial.print (measure_time);
Serial.print ("\n");
}
} // }}}
Menu <3> MainMenu ("Main menu", (char const *[]){"COD", "Nephalometry", "Debug"}, (action *[]){&doCOD, &doNeph, &debug});
// }}}
// Arduino main functions. {{{
void setup () { // {{{
Serial.begin(9600);
lcd.begin (16, 2);
pinMode (SENSOR_MAIN, OUTPUT);
pinMode (POWER, OUTPUT);
digitalWrite (SENSOR_MAIN, LOW);
digitalWrite (POWER, HIGH);
digitalWrite (SENSOR_620, LOW);
digitalWrite (SENSOR_IR, LOW);
digitalWrite (LED_620, LOW);
digitalWrite (LED_IR, LOW);
setSource (SOURCE_NONE);
for (uint8_t i = 0; i < sizeof (cal); ++i)
cal.bytes[i] = EEPROM.read (i);
} // }}}
void loop () { // {{{
MainMenu.run ();
} // }}}
// }}}