12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- /* TSL25911 Digital Light Sensor */
- /* Dynamic Range: 600M:1 */
- /* Maximum Lux: 88K */
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include "Adafruit_TSL2591.h"
- Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
- /**************************************************************************/
- /*
- Configures the gain and integration time for the TSL2561
- */
- /**************************************************************************/
- 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
-
- // 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)
- }
- /**************************************************************************/
- /*
- Program entry point for the Arduino sketch
- */
- /**************************************************************************/
- void initLuxSensor(void)
- {
- if (tsl.begin())
- {
- }
- else
- {
- //Serial.println("XNo sensor found ... check your wiring?");
- while (1);
- }
- /* Configure the sensor */
- configureSensor();
- }
- /**************************************************************************/
- /*
- Show how to read IR and Full Spectrum at once and convert to lux
- */
- /**************************************************************************/
- uint32_t readLuxSensor(void)
- {
- // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
- // That way you can do whatever math and comparisons you want!
- uint32_t lum = tsl.getFullLuminosity();
- uint16_t ir, full;
- ir = lum >> 16;
- full = lum & 0xFFFF;
- //Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
- //Serial.print("IR: "); Serial.print(ir); Serial.print(" ");
- //Serial.print("Full: "); Serial.print(full); Serial.print(" ");
- //Serial.print("Visible: "); Serial.print(full - ir); Serial.print(" ");
-
- uint32_t lux = tsl.calculateLux(full, ir);
-
- if ( (lux > 4294966000.0) ||
- (lux <-4294966000.0) )
- {
- return 0;
- }
- else {
- return lux;
- }
- }
|