source: rtos_arduino/trunk/examples/CompositeExample/Adafruit_TSL2591_Library/examples/tsl2591/tsl2591.ino@ 137

Last change on this file since 137 was 137, checked in by ertl-honda, 8 years ago

サンプルの追加.

File size: 6.7 KB
Line 
1/* TSL2591 Digital Light Sensor */
2/* Dynamic Range: 600M:1 */
3/* Maximum Lux: 88K */
4
5#include <Wire.h>
6#include <Adafruit_Sensor.h>
7#include "Adafruit_TSL2591.h"
8
9// Example for demonstrating the TSL2591 library - public domain!
10
11// connect SCL to analog 5
12// connect SDA to analog 4
13// connect Vin to 3.3-5V DC
14// connect GROUND to common ground
15
16Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
17
18/**************************************************************************/
19/*
20 Displays some basic information on this sensor from the unified
21 sensor API sensor_t type (see Adafruit_Sensor for more information)
22*/
23/**************************************************************************/
24void displaySensorDetails(void)
25{
26 sensor_t sensor;
27 tsl.getSensor(&sensor);
28 Serial.println("------------------------------------");
29 Serial.print ("Sensor: "); Serial.println(sensor.name);
30 Serial.print ("Driver Ver: "); Serial.println(sensor.version);
31 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
32 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
33 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
34 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
35 Serial.println("------------------------------------");
36 Serial.println("");
37 delay(500);
38}
39
40/**************************************************************************/
41/*
42 Configures the gain and integration time for the TSL2591
43*/
44/**************************************************************************/
45void configureSensor(void)
46{
47 // You can change the gain on the fly, to adapt to brighter/dimmer light situations
48 //tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
49 tsl.setGain(TSL2591_GAIN_MED); // 25x gain
50 //tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
51
52 // Changing the integration time gives you a longer time over which to sense light
53 // longer timelines are slower, but are good in very low light situtations!
54 tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
55 //tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
56 //tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
57 //tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
58 //tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
59 //tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
60
61 /* Display the gain and integration time for reference sake */
62 Serial.println("------------------------------------");
63 Serial.print ("Gain: ");
64 tsl2591Gain_t gain = tsl.getGain();
65 switch(gain)
66 {
67 case TSL2591_GAIN_LOW:
68 Serial.println("1x (Low)");
69 break;
70 case TSL2591_GAIN_MED:
71 Serial.println("25x (Medium)");
72 break;
73 case TSL2591_GAIN_HIGH:
74 Serial.println("428x (High)");
75 break;
76 case TSL2591_GAIN_MAX:
77 Serial.println("9876x (Max)");
78 break;
79 }
80 Serial.print ("Timing: ");
81 Serial.print((tsl.getTiming() + 1) * 100, DEC);
82 Serial.println(" ms");
83 Serial.println("------------------------------------");
84 Serial.println("");
85}
86
87/**************************************************************************/
88/*
89 Program entry point for the Arduino sketch
90*/
91/**************************************************************************/
92void setup(void)
93{
94 Serial.begin(115200);
95
96 Serial.println("Starting Adafruit TSL2591 Test!");
97
98 if (tsl.begin())
99 {
100 Serial.println("Found a TSL2591 sensor");
101 }
102 else
103 {
104 Serial.println("No sensor found ... check your wiring?");
105 while (1);
106 }
107
108 /* Display some basic information on this sensor */
109 displaySensorDetails();
110
111 /* Configure the sensor */
112 configureSensor();
113
114 // Now we're ready to get readings ... move on to loop()!
115}
116
117/**************************************************************************/
118/*
119 Shows how to perform a basic read on visible, full spectrum or
120 infrared light (returns raw 16-bit ADC values)
121*/
122/**************************************************************************/
123void simpleRead(void)
124{
125 // Simple data read example. Just read the infrared, fullspecrtrum diode
126 // or 'visible' (difference between the two) channels.
127 // This can take 100-600 milliseconds! Uncomment whichever of the following you want to read
128 uint16_t x = tsl.getLuminosity(TSL2591_VISIBLE);
129 //uint16_t x = tsl.getLuminosity(TSL2591_FULLSPECTRUM);
130 //uint16_t x = tsl.getLuminosity(TSL2591_INFRARED);
131
132 Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
133 Serial.print("Luminosity: ");
134 Serial.println(x, DEC);
135}
136
137/**************************************************************************/
138/*
139 Show how to read IR and Full Spectrum at once and convert to lux
140*/
141/**************************************************************************/
142void advancedRead(void)
143{
144 // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
145 // That way you can do whatever math and comparisons you want!
146 uint32_t lum = tsl.getFullLuminosity();
147 uint16_t ir, full;
148 ir = lum >> 16;
149 full = lum & 0xFFFF;
150 Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
151 Serial.print("IR: "); Serial.print(ir); Serial.print(" ");
152 Serial.print("Full: "); Serial.print(full); Serial.print(" ");
153 Serial.print("Visible: "); Serial.print(full - ir); Serial.print(" ");
154 Serial.print("Lux: "); Serial.println(tsl.calculateLux(full, ir));
155}
156
157/**************************************************************************/
158/*
159 Performs a read using the Adafruit Unified Sensor API.
160*/
161/**************************************************************************/
162void unifiedSensorAPIRead(void)
163{
164 /* Get a new sensor event */
165 sensors_event_t event;
166 tsl.getEvent(&event);
167
168 /* Display the results (light is measured in lux) */
169 Serial.print("[ "); Serial.print(event.timestamp); Serial.print(" ms ] ");
170 if ((event.light == 0) |
171 (event.light > 4294966000.0) |
172 (event.light <-4294966000.0))
173 {
174 /* If event.light = 0 lux the sensor is probably saturated */
175 /* and no reliable data could be generated! */
176 /* if event.light is +/- 4294967040 there was a float over/underflow */
177 Serial.println("Invalid data (adjust gain or timing)");
178 }
179 else
180 {
181 Serial.print(event.light); Serial.println(" lux");
182 }
183}
184
185/**************************************************************************/
186/*
187 Arduino loop function, called once 'setup' is complete (your own code
188 should go here)
189*/
190/**************************************************************************/
191void loop(void)
192{
193 // simpleRead();
194 // advancedRead();
195 unifiedSensorAPIRead();
196
197 delay(250);
198}
Note: See TracBrowser for help on using the repository browser.