2241 |
kaklik |
1 |
/* I2C Light Sensor |
|
|
2 |
|
|
|
3 |
The circuit: |
|
|
4 |
* analog sensors on analog ins PC0, PC1, and PC2 |
|
|
5 |
* SD card attached to SPI bus as follows: |
|
|
6 |
** MOSI/CMD - PB3 |
|
|
7 |
** MISO/DAT0 - PB4 |
|
|
8 |
** CLK - PB5 |
2242 |
kaklik |
9 |
** CS/CD/DAT3 - PD4 (4) |
|
|
10 |
|
|
|
11 |
I2C pins PC4, PC5 |
2241 |
kaklik |
12 |
|
|
|
13 |
*/ |
|
|
14 |
|
2242 |
kaklik |
15 |
#include <Wire.h> |
2241 |
kaklik |
16 |
#include <math.h> |
2242 |
kaklik |
17 |
#include <stdlib.h> |
|
|
18 |
#include <SD.h> |
2241 |
kaklik |
19 |
|
2242 |
kaklik |
20 |
#define chipSelect 4 |
2241 |
kaklik |
21 |
|
|
|
22 |
#define address 0x44 // A0 = L |
|
|
23 |
|
2246 |
kaklik |
24 |
#define LIGHT_ENABLE 1 |
|
|
25 |
#define LIGHT_DISABLE 0 |
|
|
26 |
|
2247 |
kaklik |
27 |
#define LIGHT_ONETIME 0 |
|
|
28 |
#define LIGHT_CONTINUOUS 1 |
2246 |
kaklik |
29 |
|
|
|
30 |
#define LIGHT_SENSE_VIS 0 |
|
|
31 |
#define LIGHT_SENSE_IR 1 |
|
|
32 |
|
|
|
33 |
#define LIGHT_TIMING_INTERNAL_16 0b000 |
|
|
34 |
#define LIGHT_TIMING_INTERNAL_12 0b001 |
|
|
35 |
#define LIGHT_TIMING_INTERNAL_8 0b010 |
|
|
36 |
#define LIGHT_TIMING_INTERNAL_4 0b011 |
|
|
37 |
#define LIGHT_TIMING_EXTERNAL_ADC 0b100 |
|
|
38 |
#define LIGHT_TIMING_EXTERNAL_TIMER 0b101 |
|
|
39 |
|
2247 |
kaklik |
40 |
//#define LIGHT_AUTORANGE 0 |
2246 |
kaklik |
41 |
|
2247 |
kaklik |
42 |
#define LIGHT_RANGE1 0b00 |
|
|
43 |
#define LIGHT_RANGE2 0b01 |
|
|
44 |
#define LIGHT_RANGE3 0b10 |
|
|
45 |
#define LIGHT_RANGE4 0b11 |
2241 |
kaklik |
46 |
|
2244 |
kaklik |
47 |
char filename[13]; |
|
|
48 |
int data = 0; |
|
|
49 |
int light_sensor_setup; |
|
|
50 |
|
2241 |
kaklik |
51 |
void setup() |
|
|
52 |
{ |
2244 |
kaklik |
53 |
int count=0; |
|
|
54 |
|
2241 |
kaklik |
55 |
Wire.begin(); // join i2c bus (address optional for master) |
2242 |
kaklik |
56 |
|
2241 |
kaklik |
57 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
58 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
59 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
|
|
60 |
|
|
|
61 |
Serial.print("Initializing SD card..."); |
|
|
62 |
// make sure that the default chip select pin is set to |
|
|
63 |
// output, even if you don't use it: |
|
|
64 |
pinMode(10, OUTPUT); |
|
|
65 |
|
|
|
66 |
// see if the card is present and can be initialized: |
|
|
67 |
if (!SD.begin(chipSelect)) { |
|
|
68 |
Serial.println("Card failed, or not present"); |
|
|
69 |
// don't do anything more: |
|
|
70 |
return; |
|
|
71 |
} |
|
|
72 |
Serial.println("card initialized."); |
2244 |
kaklik |
73 |
Serial.print("searching for files..."); |
|
|
74 |
|
|
|
75 |
do |
|
|
76 |
{ |
|
|
77 |
sprintf(filename,"Lux%d.txt",count); |
|
|
78 |
count++; |
|
|
79 |
} |
|
|
80 |
while(SD.exists(filename)); |
|
|
81 |
Serial.print("Using "); |
|
|
82 |
Serial.println(filename); |
2241 |
kaklik |
83 |
} |
|
|
84 |
|
|
|
85 |
void led_blink() |
|
|
86 |
{ |
|
|
87 |
digitalWrite(3, HIGH); // set the LED on |
|
|
88 |
delay(500); |
|
|
89 |
digitalWrite(3, LOW); // set the LED off |
|
|
90 |
delay(500); |
|
|
91 |
} |
|
|
92 |
|
2246 |
kaklik |
93 |
int set_light_sensor(int en, int mode, int light, int res, int range) |
2241 |
kaklik |
94 |
{ |
2246 |
kaklik |
95 |
int command=0; |
|
|
96 |
|
2247 |
kaklik |
97 |
command = (command & 0b01111111) | (en << 7); |
|
|
98 |
command = (command & 0b10111111) | (mode << 6); |
|
|
99 |
command = (command & 0b11011111) | (light << 5); |
|
|
100 |
command = (command & 0b11100011) | (res << 2); |
|
|
101 |
command = (command & 0b11111100) | (range); |
2241 |
kaklik |
102 |
|
|
|
103 |
// Setup device |
|
|
104 |
Wire.beginTransmission(address); |
|
|
105 |
Wire.send(0x00); // sends address |
|
|
106 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
|
|
107 |
Wire.endTransmission(); // stop transmitting |
2247 |
kaklik |
108 |
|
|
|
109 |
// Connect to device and set register address |
2241 |
kaklik |
110 |
Wire.beginTransmission(address); |
|
|
111 |
Wire.send(0x00); // sends address (command register) |
|
|
112 |
Wire.endTransmission(); // stop transmitting |
|
|
113 |
|
|
|
114 |
// verify written command byte |
|
|
115 |
Wire.beginTransmission(address); |
|
|
116 |
Wire.requestFrom(address, 1); |
|
|
117 |
if (command != Wire.receive()) |
|
|
118 |
{ |
2247 |
kaklik |
119 |
Serial.println("Error in sensor setting"); |
2241 |
kaklik |
120 |
return 4; |
|
|
121 |
} |
|
|
122 |
Wire.endTransmission(); // stop transmitting |
|
|
123 |
light_sensor_setup=command; |
2247 |
kaklik |
124 |
Serial.println(command, BIN); |
|
|
125 |
|
2246 |
kaklik |
126 |
return 0; |
2241 |
kaklik |
127 |
} |
|
|
128 |
|
|
|
129 |
float get_light_measurement() |
|
|
130 |
{ |
|
|
131 |
int ret=0; |
|
|
132 |
|
|
|
133 |
// Connect to device and set register address |
|
|
134 |
Wire.beginTransmission(address); |
|
|
135 |
Wire.send(0x01); // sends address of LSB reagister |
|
|
136 |
Wire.endTransmission(); // stop transmitting |
|
|
137 |
|
2247 |
kaklik |
138 |
// Connect to device and request first byte |
2241 |
kaklik |
139 |
Wire.beginTransmission(address); |
|
|
140 |
Wire.requestFrom(address, 1); |
|
|
141 |
ret = Wire.receive(); |
|
|
142 |
Wire.endTransmission(); // stop transmitting |
|
|
143 |
|
|
|
144 |
// Connect to device and set register address |
|
|
145 |
Wire.beginTransmission(address); |
|
|
146 |
Wire.send(0x02); // sends address of MSB register |
|
|
147 |
Wire.endTransmission(); // stop transmitting |
|
|
148 |
|
2247 |
kaklik |
149 |
// Connect to device and request second byte |
2241 |
kaklik |
150 |
Wire.beginTransmission(address); |
|
|
151 |
Wire.requestFrom(address, 1); |
|
|
152 |
ret +=256 * Wire.receive(); |
|
|
153 |
Wire.endTransmission(); // stop transmitting |
|
|
154 |
|
2247 |
kaklik |
155 |
return (64000.0/pow(2.0,16)*ret); |
2241 |
kaklik |
156 |
} |
|
|
157 |
|
|
|
158 |
void loop() |
|
|
159 |
{ |
|
|
160 |
float luxVIS=0; |
|
|
161 |
float luxIR=0; |
2243 |
kaklik |
162 |
float time; |
2241 |
kaklik |
163 |
|
2247 |
kaklik |
164 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); //setup sensor for visible measuring |
2241 |
kaklik |
165 |
led_blink(); // Delay for measurement |
|
|
166 |
luxVIS=get_light_measurement(); |
|
|
167 |
|
2244 |
kaklik |
168 |
time=millis()/1000.0; |
2243 |
kaklik |
169 |
|
2247 |
kaklik |
170 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); // setup sensor for infrared measuring |
2241 |
kaklik |
171 |
led_blink(); // Delay for measurement |
|
|
172 |
luxIR=get_light_measurement(); |
|
|
173 |
|
|
|
174 |
// 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. |
2244 |
kaklik |
176 |
File dataFile = SD.open(filename, FILE_WRITE); |
2241 |
kaklik |
177 |
|
|
|
178 |
// if the file is available, write to it: |
|
|
179 |
if (dataFile) { |
2243 |
kaklik |
180 |
dataFile.print("$LUX0.1 "); |
|
|
181 |
dataFile.print(time,3); |
|
|
182 |
dataFile.print(" "); |
|
|
183 |
dataFile.print(luxVIS,3); |
|
|
184 |
dataFile.print(" "); |
|
|
185 |
dataFile.println(luxIR,3); |
2241 |
kaklik |
186 |
dataFile.close(); |
2243 |
kaklik |
187 |
|
2241 |
kaklik |
188 |
// print to the serial port too: |
2243 |
kaklik |
189 |
Serial.print("$LUX0.1 "); |
|
|
190 |
Serial.print(time,3); |
|
|
191 |
Serial.print(" "); |
2242 |
kaklik |
192 |
Serial.print(luxVIS,3); |
|
|
193 |
Serial.print(" "); |
|
|
194 |
Serial.println(luxIR,3); |
2241 |
kaklik |
195 |
} |
|
|
196 |
// if the file isn't open, pop up an error: |
|
|
197 |
else { |
2244 |
kaklik |
198 |
Serial.print("error opening "); |
|
|
199 |
Serial.println(filename); |
2241 |
kaklik |
200 |
} |
|
|
201 |
} |
|
|
202 |
|