Rev 2247 Rev 2248
Line 28... Line 28...
28 #define LIGHT_CONTINUOUS 1 28 #define LIGHT_CONTINUOUS 1
29   29  
30 #define LIGHT_SENSE_VIS 0 30 #define LIGHT_SENSE_VIS 0
31 #define LIGHT_SENSE_IR 1 31 #define LIGHT_SENSE_IR 1
32   32  
33 #define LIGHT_TIMING_INTERNAL_16 0b000 33 #define LIGHT_INTERNAL_16 0b00000000
34 #define LIGHT_TIMING_INTERNAL_12 0b001 34 #define LIGHT_INTERNAL_12 0b00000100
35 #define LIGHT_TIMING_INTERNAL_8 0b010 35 #define LIGHT_INTERNAL_8 0b00001000
36 #define LIGHT_TIMING_INTERNAL_4 0b011 36 #define LIGHT_INTERNAL_4 0b00001100
37 #define LIGHT_TIMING_EXTERNAL_ADC 0b100 37 #define LIGHT_EXTERNAL_ADC 0b00010000
38 #define LIGHT_TIMING_EXTERNAL_TIMER 0b101 38 #define LIGHT_EXTERNAL_TIMER 0b00010100
39   39  
40 //#define LIGHT_AUTORANGE 0 40 //#define LIGHT_AUTORANGE 0
41   41  
42 #define LIGHT_RANGE1 0b00 42 #define LIGHT_RANGE1 0b00000000
43 #define LIGHT_RANGE2 0b01 43 #define LIGHT_RANGE2 0b00000001
44 #define LIGHT_RANGE3 0b10 44 #define LIGHT_RANGE3 0b00000010
45 #define LIGHT_RANGE4 0b11 45 #define LIGHT_RANGE4 0b00000011
46   46  
47 char filename[13]; 47 char filename[13];
48 int data = 0; -  
49 int light_sensor_setup; -  
50   48  
51 void setup() 49 void setup()
52 { 50 {
53 int count=0; 51 int count=0;
54   52  
Line 88... Line 86...
88 delay(500); 86 delay(500);
89 digitalWrite(3, LOW); // set the LED off 87 digitalWrite(3, LOW); // set the LED off
90 delay(500); 88 delay(500);
91 } 89 }
92   90  
-   91 /*int light_sensor_write( unsigned int data, unsigned int address)
-   92 {
-   93 }
-   94 */
-   95  
-   96  
93 int set_light_sensor(int en, int mode, int light, int res, int range) 97 int set_light_sensor(int en, int mode, int light, int res, int range)
94 { 98 {
95 int command=0; 99 int command=0;
96   100  
97 command = (command & 0b01111111) | (en << 7); 101 command = (command & 0b01111111) | (en << 7);
Line 108... Line 112...
108 112
109 // Connect to device and set register address 113 // Connect to device and set register address
110 Wire.beginTransmission(address); 114 Wire.beginTransmission(address);
111 Wire.send(0x00); // sends address (command register) 115 Wire.send(0x00); // sends address (command register)
112 Wire.endTransmission(); // stop transmitting 116 Wire.endTransmission(); // stop transmitting
113 -  
114 // verify written command byte 117 // verify written command byte
115 Wire.beginTransmission(address); 118 Wire.beginTransmission(address);
116 Wire.requestFrom(address, 1); 119 Wire.requestFrom(address, 1);
117 if (command != Wire.receive()) 120 if (command != Wire.receive())
118 { 121 {
119 Serial.println("Error in sensor setting"); 122 Serial.println("Error in sensor setting");
120 return 4; 123 return 4;
121 } 124 }
122 Wire.endTransmission(); // stop transmitting 125 Wire.endTransmission(); // stop transmitting
123 light_sensor_setup=command; -  
124 Serial.println(command, BIN); -  
125 -  
126 return 0; 126 return 0;
127 } 127 }
128   128  
129 float get_light_measurement() 129 float get_light_measurement()
130 { 130 {
131 int ret=0; 131 unsigned int ret=0;
-   132 unsigned int setting=0; // variable to storage readed settings
-   133  
-   134 unsigned long int resolution; // parsed ADC bit resolution
-   135 unsigned int range; // parsed measurement range
132   136  
133 // Connect to device and set register address 137 // Connect to device and set register address
134 Wire.beginTransmission(address); 138 Wire.beginTransmission(address);
135 Wire.send(0x01); // sends address of LSB reagister 139 Wire.send(0x01); // sends address of LSB reagister
136 Wire.endTransmission(); // stop transmitting 140 Wire.endTransmission(); // stop transmitting
Line 150... Line 154...
150 Wire.beginTransmission(address); 154 Wire.beginTransmission(address);
151 Wire.requestFrom(address, 1); 155 Wire.requestFrom(address, 1);
152 ret +=256 * Wire.receive(); 156 ret +=256 * Wire.receive();
153 Wire.endTransmission(); // stop transmitting 157 Wire.endTransmission(); // stop transmitting
154   158  
-   159  
-   160 // Connect to device and set register address
-   161 Wire.beginTransmission(address);
-   162 Wire.send(0x00); // sends address (command register)
-   163 Wire.endTransmission(); // stop transmitting
-   164 // get sensor setting
-   165 Wire.beginTransmission(address);
-   166 Wire.requestFrom(address, 1);
155 return (64000.0/pow(2.0,16)*ret); 167 setting = Wire.receive();
-   168 Wire.endTransmission(); // stop transmitting
-   169
-   170 switch (setting & 0b00011100) // determine ADC resolution
-   171 {
-   172 case LIGHT_INTERNAL_16:
-   173 resolution=65536;
-   174 break;
-   175
-   176 case LIGHT_INTERNAL_12:
-   177 resolution=4096;
-   178 break;
-   179
-   180 case LIGHT_INTERNAL_8:
-   181 resolution=256;
-   182 break;
-   183
-   184 case LIGHT_INTERNAL_4:
-   185 resolution=16;
-   186 break;
-   187 }
-   188
-   189 switch (setting & 0b00000011) // determine measurement range
-   190 {
-   191 case LIGHT_RANGE1:
-   192 range=1000;
-   193 break;
-   194
-   195 case LIGHT_RANGE2:
-   196 range=4000;
-   197 break;
-   198
-   199 case LIGHT_RANGE3:
-   200 range=16000;
-   201 break;
-   202
-   203 case LIGHT_RANGE4:
-   204 range=64000;
-   205 break;
-   206 }
-   207 return ((float)range/resolution*ret); // calculate output value
156 } 208 }
157   209  
158 void loop() 210 void loop()
159 { 211 {
160 float luxVIS=0; 212 float luxVIS=0;
161 float luxIR=0; 213 float luxIR=0;
162 float time; 214 float time;
163   215  
164 set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); //setup sensor for visible measuring 216 set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_INTERNAL_16, LIGHT_RANGE2); //setup sensor for visible measuring
165 led_blink(); // Delay for measurement 217 led_blink(); // Delay for measurement
166 luxVIS=get_light_measurement(); 218 luxVIS=get_light_measurement();
167   219  
168 time=millis()/1000.0; 220 time=millis()/1000.0;
169 221
170 set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); // setup sensor for infrared measuring 222 set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_INTERNAL_16, LIGHT_RANGE2); // setup sensor for infrared measuring
171 led_blink(); // Delay for measurement 223 led_blink(); // Delay for measurement
172 luxIR=get_light_measurement(); 224 luxIR=get_light_measurement();
173 225
174 // open the file. note that only one file can be open at a time, 226 // open the file. note that only one file can be open at a time,
175 // so you have to close this one before opening another. 227 // so you have to close this one before opening another.