-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.ino
341 lines (303 loc) · 9.78 KB
/
arduino.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
/* TSL2591 Digital Light Sensor */
/* Dynamic Range: 600M:1 */
/* Maximum Lux: 88K */
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
// connect SCL to analog 5
// connect SDA to analog 4
// connect Vin to 3.3-5V DC
// connect GROUND to common ground
#define BLUE_LED 4
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
bool sensorFound = false;
/**************************************************************************/
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
/**************************************************************************/
/*
Configures the gain and integration time for the TSL2591
*/
/**************************************************************************/
void configureSensor(void)
{
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
//tsl.setGain(TSL2591_GAIN_MED); // 25x gain
//tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
tsl.setGain(TSL2591_GAIN_MAX);
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
//tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
//tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
//tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
//tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
//tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
/* Display the gain and integration time for reference sake */
//Serial.println("------------------------------------");
//Serial.print ("Gain: ");
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
//Serial.println("1x (Low)");
break;
case TSL2591_GAIN_MED:
//Serial.println("25x (Medium)");
break;
case TSL2591_GAIN_HIGH:
//Serial.println("428x (High)");
break;
case TSL2591_GAIN_MAX:
//Serial.println("9876x (Max)");
break;
}
//Serial.print ("Timing: ");
//Serial.print((tsl.getTiming() + 1) * 100, DEC);
//Serial.println(" ms");
//Serial.println("------------------------------------");
//Serial.println("");
}
/**************************************************************************/
/*
Program entry point for the Arduino sketch
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
if (tsl.begin())
{
Serial.println("Found a TSL2591 sensor");
sensorFound = true;
displaySensorDetails();
configureSensor();
pinMode(BLUE_LED, OUTPUT);
}
else
{
Serial.println("No sensor found ... check your wiring?");
}
}
int read_sensor(int gain)
{
if (gain == TSL2591_GAIN_LOW ||
gain == TSL2591_GAIN_MED ||
gain == TSL2591_GAIN_HIGH ||
gain == TSL2591_GAIN_MAX) {
// if a valid value was given for gain, set it in the sensor before reading
tsl.setGain(gain);
} else {
// do nothing, just leave the gain as it is currently configured
}
return read_sensor();
}
uint16_t read_sensor() {
/* Get a new sensor event */
uint16_t lumi = tsl.getLuminosity(TSL2591_VISIBLE);
return lumi;
sensors_event_t event;
tsl.getEvent(&event);
if (//(event.light == 0) |
(event.light > 4294966000.0) |
(event.light <-4294966000.0))
{
/* If event.light = 0 lux the sensor is probably saturated */
/* and no reliable data could be generated! */
/* if event.light is +/- 4294967040 there was a float over/underflow */
return -1;
}
else
{
return (int)event.light;
}
}
#define ERR_NO_SENSOR -1
#define ERR_INVALID_PARAMETER -2
#define ERR_OVERFLOW -3
int32_t raw_read(uint8_t gain) {
if (!sensorFound)
{
return -1;
}
if ((gain != TSL2591_GAIN_LOW) &&
(gain != TSL2591_GAIN_MED) &&
(gain != TSL2591_GAIN_HIGH) &&
(gain != TSL2591_GAIN_MAX)) {
return -2;
}
// Enable the device
tsl.enable();
tsl.write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, tsl.getTiming() | gain);
// Wait for ADC to complete
for (uint8_t d = 0; d <= tsl.getTiming(); d++)
{
delay(120);
}
uint16_t x = tsl.read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
tsl.disable();
if (x == 0xFFFF) {
return ERR_OVERFLOW;
}
return x;
}
bool led_status = 0;
#define TSL2591_GMAX_FACTOR 9876
#define TSL2591_GHIGH_FACTOR 428
#define TSL2591_GMED_FACTOR 25
#define TSL2591_GLOW_FACTOR 1
bool doMeasure = false;
String sampleName;
long numRep = 0;
long repCount = 0;
void stopMeasurement() {
doMeasure = false;
sampleName = "";
numRep = 0;
repCount = 0;
digitalWrite(BLUE_LED, LOW);
}
void loop(void)
{
if (Serial.available() > 0) {
String command = Serial.readString();
if (command.compareTo("stop") == 0) {
stopMeasurement();
} else if (command.startsWith("start")) {
int sampleStart = command.indexOf(' ');
if (sampleStart < 0) {
Serial.println("Invalid command");
return;
}
sampleStart += 1;
int repStart = command.indexOf(' ', sampleStart);
if (repStart < 0) {
Serial.println("Invalid command");
return;
}
repStart += 1;
sampleName = command.substring(sampleStart, repStart - 1);
String repStr = command.substring(repStart);
numRep = repStr.toInt();
repCount = 0;
doMeasure = true;
digitalWrite(BLUE_LED, HIGH);
}
}
if (numRep <= repCount) { // we're done
stopMeasurement();
}
if (sensorFound && doMeasure && numRep > repCount) {
repCount++;
int32_t v = raw_read(TSL2591_GAIN_MAX);
if (v < 0) {
if (v == ERR_NO_SENSOR) {
Serial.println("Error reading sensor: No TSL2591 sensor present");
stopMeasurement();
return;
} else if (v == ERR_INVALID_PARAMETER) {
Serial.println("Error reading sensor: Invalid gain parameter");
stopMeasurement();
return;
} else {
// Overflow. Fall through and try again with a different gain setting.
}
} else {
// Good, we got a useable value.
Serial.print(sampleName + ",");
Serial.print(repCount); Serial.print(",");
Serial.println(v);
return;
}
v = raw_read(TSL2591_GAIN_HIGH);
if (v < 0) {
if (v == ERR_NO_SENSOR) {
Serial.println("Error reading sensor: No TSL2591 sensor present");
stopMeasurement();
return;
} else if (v == ERR_INVALID_PARAMETER) {
Serial.println("Error reading sensor: Invalid gain parameter");
stopMeasurement();
return;
} else {
// Overflow. Fall through and try again with a different gain setting.
}
} else {
// Good, we got a useable value.
// Adjust it for gain factor so we end up with a single scale for all values.
v *= (TSL2591_GMAX_FACTOR / TSL2591_GHIGH_FACTOR);
Serial.print(sampleName + ",");
Serial.print(repCount); Serial.print(",");
Serial.println(v);
return;
}
v = raw_read(TSL2591_GAIN_MED);
if (v < 0) {
if (v == ERR_NO_SENSOR) {
Serial.println("Error reading sensor: No TSL2591 sensor present");
stopMeasurement();
return;
} else if (v == ERR_INVALID_PARAMETER) {
Serial.println("Error reading sensor: Invalid gain parameter");
stopMeasurement();
return;
} else {
// Overflow. Fall through and try again with a different gain setting.
}
} else {
// Good, we got a useable value.
// Adjust it for gain factor so we end up with a single scale for all values.
v *= (TSL2591_GHIGH_FACTOR / TSL2591_GMED_FACTOR);
Serial.print(sampleName + ",");
Serial.print(repCount); Serial.print(",");
Serial.println(v);
return;
}
v = raw_read(TSL2591_GAIN_LOW);
if (v < 0) {
if (v == ERR_NO_SENSOR) {
Serial.println("Error reading sensor: No TSL2591 sensor present");
stopMeasurement();
return;
} else if (v == ERR_INVALID_PARAMETER) {
Serial.println("Error reading sensor: Invalid gain parameter");
stopMeasurement();
return;
} else {
// Overflow. Fall through and try again with a different gain setting.
}
} else {
// Good, we got a useable value.
// Adjust it for gain factor so we end up with a single scale for all values.
v *= (TSL2591_GMED_FACTOR / TSL2591_GLOW_FACTOR);
Serial.print(sampleName + ",");
Serial.print(repCount); Serial.print(",");
Serial.println(v);
return;
}
} else {
// If no sensor was found, just blink the LED to signal this
digitalWrite(LED_BUILTIN, led_status);
led_status = (led_status + 1) % 2;
delay(500);
}
}