-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSL2561_Functions.ino
96 lines (86 loc) · 3.18 KB
/
TSL2561_Functions.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
void displayTSL2561SensorDetails(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("------------------------------------");
delay(500);
}
/**************************************************************************/
/*
Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
// tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above!
Serial.print ("Gain: "); Serial.println("Auto");
Serial.print ("Timing: "); Serial.println("402 ms");
Serial.println("------------------------------------"); */
}
void ReadTSL2561()
{
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event); //TSL2561 Lux sensor
/* Display the results (light is measured in lux) */
if (event.light)
{
TSL2561Val = event.light;
if ( TSL2561Val >= 17000 )
{
errorFeed->save("TSL2561 sensor saturated"); //Log error to Adafruit IO
ERRORLOG = SD.open("error.txt", FILE_WRITE);
if (ERRORLOG)
{
TimeStampSD(ERRORLOG);
ERRORLOG.println("TSL2561 Sensor Error: lux above sensor max threshold");
ERRORLOG.close();
}
}
}
else
{
/* If event.light = 0 lux during sun-down, there is likely not enough light for the sensor to read */
TSL2561Val = 0;
}
}
void WriteTSL2561Serial()
{
Serial.print("Lux: ");
Serial.print(TSL2561Val);
Serial.println(" Lux");
}
void WriteTSL2561SD()
{
DATALOG.print(TSL2561Val);
DATALOG.print(",");
}
void WriteTSL2561IO()
{
luxFeed->save(TSL2561Val);
}
void WriteTSL2561OLED()
{
display.setTextSize(1); //textsize 1 = 7 px high
display.setTextColor(WHITE);
display.setCursor(0,16);
display.print("Lux: ");
display.print(TSL2561Val);
display.println(" Lux");
display.display(); // NOTE: You _must_ call display after making any drawing commands
}