-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIR_sensor_demo.pde
107 lines (85 loc) · 2.65 KB
/
NIR_sensor_demo.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
//
// This Arduino sketch reads our custom NIR absorption sensor.
//
// This code is part of the BioBridge Project.
//
//
// author: rolf van widenfelt (c) 2011
//
// revision history:
//
// apr 25, 2011 - rolf
// identify this probe. (needed for BioBoard protocol)
//
// apr 17, 2011 - rolf
// created.
// ADC code seems to work.. still need to connect actual sensor and document connections!
//
//
// code modification:
// you will need to set some calibration points... (FILL THIS IN!!)
//
// description:
// this sketch will periodically output a short string that contains the NIR transmittance
// along with some other fields that indicate which probe (0) is being read.
// the string should look like this for a transmittance of 99% :
//
// @NIR:0:0.99$
//
// this is output periodically. (in this case, every 5 sec)
//
// In the case of an error, an "E" message is output instead of the temperature, like this:
//
// @TC:0:EBADVALUE$
//
// note that these data packets are output to the serial console window at 19200 baud.
// also, when the sketch first starts, it identifies the software and version, like this:
//
// @ID:BIOBOARD:TESTBATCH1:1.1$
//
// that's it!
//
static const char ProjectName[] = "TESTBATCH1";
const int analogNIRPin = A0; // analog input pin that the NIR phototransistor circuit is connected to
// CALIBRATION SETTINGS
#define IMAX 4.9 /* max phototransistor current with IR LED on (no obstructions, just 1inch air) */
#define IMIN 0.02 /* dark current (NOTUSED) */
#define VMAX 5.0 /* arduino voltage = 5.0v */
#define ADCMAX 1023 /* highest ADC value */
#define IMAXI (ADCMAX*IMAX/VMAX) /* highest ADC value we expect from our sensor */
#define IMAXI_INV (1.0/(ADCMAX*IMAX/VMAX)) /* inverse (this avoids a divide during runtime) */
void setup(void)
{
// start serial port
//Serial.begin(9600);
Serial.begin(19200);
Serial.print("\n\r@ID:BIOBOARD:");
Serial.print(ProjectName);
Serial.print(":0.1$\n\r");
// configure ADC to use external 5v reference (default)
analogReference(DEFAULT);
// just in case, throw away 1st ADC read
(void) analogRead(analogNIRPin);
// identify this probe
Serial.print("@PR:NIR:0$\n\r");
}
void printNIR()
{
int sensor = analogRead(analogNIRPin);
float sample = IMAXI_INV * sensor;
if (sensor > IMAXI + 10) {
Serial.print("EBADVALUE"); // oops, value is clearly wrong, so output an "E" message.
} else {
Serial.print(sample);
}
// XXX debug - we output raw ADC value as well
Serial.print(":");
Serial.print(sensor);
}
void loop(void)
{
delay(2500);
Serial.print("@NIR:0:");
printNIR();
Serial.print("$\n\r");
}