| Line 22... |
Line 22... |
| 22 |
#define address 0x44 // A0 = L |
22 |
#define address 0x44 // A0 = L |
| 23 |
|
23 |
|
| 24 |
#define LIGHT_ENABLE 1 |
24 |
#define LIGHT_ENABLE 1 |
| 25 |
#define LIGHT_DISABLE 0 |
25 |
#define LIGHT_DISABLE 0 |
| 26 |
|
26 |
|
| 27 |
#define LIGHT_MEASUREMENT_ONETIME 0 |
27 |
#define LIGHT_ONETIME 0 |
| 28 |
#define LIGHT_MEASUREMENT_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_TIMING_INTERNAL_16 0b000 |
| Line 35... |
Line 35... |
| 35 |
#define LIGHT_TIMING_INTERNAL_8 0b010 |
35 |
#define LIGHT_TIMING_INTERNAL_8 0b010 |
| 36 |
#define LIGHT_TIMING_INTERNAL_4 0b011 |
36 |
#define LIGHT_TIMING_INTERNAL_4 0b011 |
| 37 |
#define LIGHT_TIMING_EXTERNAL_ADC 0b100 |
37 |
#define LIGHT_TIMING_EXTERNAL_ADC 0b100 |
| 38 |
#define LIGHT_TIMING_EXTERNAL_TIMER 0b101 |
38 |
#define LIGHT_TIMING_EXTERNAL_TIMER 0b101 |
| 39 |
|
39 |
|
| 40 |
#define LIGHT_AUTORANGE 0 |
40 |
//#define LIGHT_AUTORANGE 0 |
| 41 |
|
41 |
|
| 42 |
#define LIGHT_RANGE1 1 |
42 |
#define LIGHT_RANGE1 0b00 |
| 43 |
#define LIGHT_RANGE2 2 |
43 |
#define LIGHT_RANGE2 0b01 |
| 44 |
#define LIGHT_RANGE3 3 |
44 |
#define LIGHT_RANGE3 0b10 |
| 45 |
#define LIGHT_RANGE4 4 |
45 |
#define LIGHT_RANGE4 0b11 |
| 46 |
|
46 |
|
| 47 |
char filename[13]; |
47 |
char filename[13]; |
| 48 |
int data = 0; |
48 |
int data = 0; |
| 49 |
int light_sensor_setup; |
49 |
int light_sensor_setup; |
| 50 |
|
50 |
|
| Line 92... |
Line 92... |
| 92 |
|
92 |
|
| 93 |
int set_light_sensor(int en, int mode, int light, int res, int range) |
93 |
int set_light_sensor(int en, int mode, int light, int res, int range) |
| 94 |
{ |
94 |
{ |
| 95 |
int command=0; |
95 |
int command=0; |
| 96 |
|
96 |
|
| 97 |
commad= (en << 7 & 0b10000000); |
97 |
command = (command & 0b01111111) | (en << 7); |
| 98 |
|
- |
|
| 99 |
switch (light) |
- |
|
| 100 |
{ |
- |
|
| 101 |
case LIGHT_SENSE_VIS: |
98 |
command = (command & 0b10111111) | (mode << 6); |
| 102 |
{ |
- |
|
| 103 |
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) |
99 |
command = (command & 0b11011111) | (light << 5); |
| 104 |
break; |
- |
|
| 105 |
} |
- |
|
| 106 |
|
- |
|
| 107 |
case LIGHT_SENSE_IR: |
100 |
command = (command & 0b11100011) | (res << 2); |
| 108 |
{ |
- |
|
| 109 |
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) |
101 |
command = (command & 0b11111100) | (range); |
| 110 |
break; |
- |
|
| 111 |
} |
- |
|
| 112 |
|
- |
|
| 113 |
default: |
- |
|
| 114 |
return 3; |
- |
|
| 115 |
} |
- |
|
| 116 |
|
102 |
|
| 117 |
// Setup device |
103 |
// Setup device |
| 118 |
Wire.beginTransmission(address); |
104 |
Wire.beginTransmission(address); |
| 119 |
Wire.send(0x00); // sends address |
105 |
Wire.send(0x00); // sends address |
| 120 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
106 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
| 121 |
Wire.endTransmission(); // stop transmitting |
107 |
Wire.endTransmission(); // stop transmitting |
| 122 |
|
- |
|
| 123 |
|
108 |
|
| 124 |
// Connect to device and set register address |
109 |
// Connect to device and set register address |
| 125 |
Wire.beginTransmission(address); |
110 |
Wire.beginTransmission(address); |
| 126 |
Wire.send(0x00); // sends address (command register) |
111 |
Wire.send(0x00); // sends address (command register) |
| 127 |
Wire.endTransmission(); // stop transmitting |
112 |
Wire.endTransmission(); // stop transmitting |
| 128 |
|
113 |
|
| 129 |
// verify written command byte |
114 |
// verify written command byte |
| 130 |
Wire.beginTransmission(address); |
115 |
Wire.beginTransmission(address); |
| 131 |
Wire.requestFrom(address, 1); |
116 |
Wire.requestFrom(address, 1); |
| 132 |
if (command != Wire.receive()) |
117 |
if (command != Wire.receive()) |
| 133 |
{ |
118 |
{ |
| 134 |
Serial.println(data, BIN); |
119 |
Serial.println("Error in sensor setting"); |
| 135 |
return 4; |
120 |
return 4; |
| 136 |
} |
121 |
} |
| 137 |
Wire.endTransmission(); // stop transmitting |
122 |
Wire.endTransmission(); // stop transmitting |
| 138 |
light_sensor_setup=command; |
123 |
light_sensor_setup=command; |
| - |
|
124 |
Serial.println(command, BIN); |
| - |
|
125 |
|
| 139 |
return 0; |
126 |
return 0; |
| 140 |
} |
127 |
} |
| 141 |
|
128 |
|
| 142 |
float get_light_measurement() |
129 |
float get_light_measurement() |
| 143 |
{ |
130 |
{ |
| Line 146... |
Line 133... |
| 146 |
// Connect to device and set register address |
133 |
// Connect to device and set register address |
| 147 |
Wire.beginTransmission(address); |
134 |
Wire.beginTransmission(address); |
| 148 |
Wire.send(0x01); // sends address of LSB reagister |
135 |
Wire.send(0x01); // sends address of LSB reagister |
| 149 |
Wire.endTransmission(); // stop transmitting |
136 |
Wire.endTransmission(); // stop transmitting |
| 150 |
|
137 |
|
| 151 |
// Connect to device and request one byte |
138 |
// Connect to device and request first byte |
| 152 |
Wire.beginTransmission(address); |
139 |
Wire.beginTransmission(address); |
| 153 |
Wire.requestFrom(address, 1); |
140 |
Wire.requestFrom(address, 1); |
| 154 |
ret = Wire.receive(); |
141 |
ret = Wire.receive(); |
| 155 |
Wire.endTransmission(); // stop transmitting |
142 |
Wire.endTransmission(); // stop transmitting |
| 156 |
|
143 |
|
| 157 |
// Connect to device and set register address |
144 |
// Connect to device and set register address |
| 158 |
Wire.beginTransmission(address); |
145 |
Wire.beginTransmission(address); |
| 159 |
Wire.send(0x02); // sends address of MSB register |
146 |
Wire.send(0x02); // sends address of MSB register |
| 160 |
Wire.endTransmission(); // stop transmitting |
147 |
Wire.endTransmission(); // stop transmitting |
| 161 |
|
148 |
|
| 162 |
// Connect to device and request one byte |
149 |
// Connect to device and request second byte |
| 163 |
Wire.beginTransmission(address); |
150 |
Wire.beginTransmission(address); |
| 164 |
Wire.requestFrom(address, 1); |
151 |
Wire.requestFrom(address, 1); |
| 165 |
ret +=256 * Wire.receive(); |
152 |
ret +=256 * Wire.receive(); |
| 166 |
Wire.endTransmission(); // stop transmitting |
153 |
Wire.endTransmission(); // stop transmitting |
| 167 |
|
154 |
|
| 168 |
return (1000.0/pow(2.0,16)*ret); |
155 |
return (64000.0/pow(2.0,16)*ret); |
| 169 |
} |
156 |
} |
| 170 |
|
157 |
|
| 171 |
void loop() |
158 |
void loop() |
| 172 |
{ |
159 |
{ |
| 173 |
float luxVIS=0; |
160 |
float luxVIS=0; |
| 174 |
float luxIR=0; |
161 |
float luxIR=0; |
| 175 |
float time; |
162 |
float time; |
| 176 |
|
163 |
|
| 177 |
set_light_sensor(LIGHT_SENSE_VIS); //setup sensor for visible measuring |
164 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); //setup sensor for visible measuring |
| 178 |
led_blink(); // Delay for measurement |
165 |
led_blink(); // Delay for measurement |
| 179 |
luxVIS=get_light_measurement(); |
166 |
luxVIS=get_light_measurement(); |
| 180 |
|
167 |
|
| 181 |
time=millis()/1000.0; |
168 |
time=millis()/1000.0; |
| 182 |
|
169 |
|
| 183 |
set_light_sensor(LIGHT_SENSE_IR); // setup sensor for infrared measuring |
170 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); // setup sensor for infrared measuring |
| 184 |
led_blink(); // Delay for measurement |
171 |
led_blink(); // Delay for measurement |
| 185 |
luxIR=get_light_measurement(); |
172 |
luxIR=get_light_measurement(); |
| 186 |
|
173 |
|
| 187 |
// open the file. note that only one file can be open at a time, |
174 |
// open the file. note that only one file can be open at a time, |
| 188 |
// so you have to close this one before opening another. |
175 |
// so you have to close this one before opening another. |