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 |
#define SENSE_VIS 0 |
|
|
24 |
#define SENSE_IR 1 |
|
|
25 |
|
|
|
26 |
#define LIGHT_AUTORANGE 0 |
|
|
27 |
#define LIGHT_RANGE1 1 |
|
|
28 |
#define LIGHT_RANGE2 2 |
|
|
29 |
#define LIGHT_RANGE3 3 |
|
|
30 |
#define LIGHT_RANGE4 4 |
|
|
31 |
|
2244 |
kaklik |
32 |
char filename[13]; |
|
|
33 |
int data = 0; |
|
|
34 |
int light_sensor_setup; |
|
|
35 |
|
2241 |
kaklik |
36 |
void setup() |
|
|
37 |
{ |
2244 |
kaklik |
38 |
int count=0; |
|
|
39 |
|
2241 |
kaklik |
40 |
Wire.begin(); // join i2c bus (address optional for master) |
2242 |
kaklik |
41 |
|
2241 |
kaklik |
42 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
43 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
44 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
|
|
45 |
|
|
|
46 |
Serial.print("Initializing SD card..."); |
|
|
47 |
// make sure that the default chip select pin is set to |
|
|
48 |
// output, even if you don't use it: |
|
|
49 |
pinMode(10, OUTPUT); |
|
|
50 |
|
|
|
51 |
// see if the card is present and can be initialized: |
|
|
52 |
if (!SD.begin(chipSelect)) { |
|
|
53 |
Serial.println("Card failed, or not present"); |
|
|
54 |
// don't do anything more: |
|
|
55 |
return; |
|
|
56 |
} |
|
|
57 |
Serial.println("card initialized."); |
2244 |
kaklik |
58 |
Serial.print("searching for files..."); |
|
|
59 |
|
|
|
60 |
do |
|
|
61 |
{ |
|
|
62 |
sprintf(filename,"Lux%d.txt",count); |
|
|
63 |
count++; |
|
|
64 |
} |
|
|
65 |
while(SD.exists(filename)); |
|
|
66 |
Serial.print("Using "); |
|
|
67 |
Serial.println(filename); |
2241 |
kaklik |
68 |
} |
|
|
69 |
|
|
|
70 |
void led_blink() |
|
|
71 |
{ |
|
|
72 |
digitalWrite(3, HIGH); // set the LED on |
|
|
73 |
delay(500); |
|
|
74 |
digitalWrite(3, LOW); // set the LED off |
|
|
75 |
delay(500); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
int set_light_sensor(int mode) |
|
|
79 |
{ |
|
|
80 |
int command; |
|
|
81 |
|
|
|
82 |
switch (mode) |
|
|
83 |
{ |
|
|
84 |
case SENSE_VIS: |
|
|
85 |
{ |
|
|
86 |
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) |
|
|
87 |
break; |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
case SENSE_IR: |
|
|
91 |
{ |
|
|
92 |
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
93 |
break; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
default: |
|
|
97 |
return 3; |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
// Setup device |
|
|
101 |
Wire.beginTransmission(address); |
|
|
102 |
Wire.send(0x00); // sends address |
|
|
103 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
|
|
104 |
Wire.endTransmission(); // stop transmitting |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
// Connect to device and set register address |
|
|
108 |
Wire.beginTransmission(address); |
|
|
109 |
Wire.send(0x00); // sends address (command register) |
|
|
110 |
Wire.endTransmission(); // stop transmitting |
|
|
111 |
|
|
|
112 |
// verify written command byte |
|
|
113 |
Wire.beginTransmission(address); |
|
|
114 |
Wire.requestFrom(address, 1); |
|
|
115 |
if (command != Wire.receive()) |
|
|
116 |
{ |
|
|
117 |
return 4; |
|
|
118 |
Serial.print(data, BIN); |
|
|
119 |
} |
|
|
120 |
Wire.endTransmission(); // stop transmitting |
|
|
121 |
light_sensor_setup=command; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
float get_light_measurement() |
|
|
125 |
{ |
|
|
126 |
int ret=0; |
|
|
127 |
|
|
|
128 |
// Connect to device and set register address |
|
|
129 |
Wire.beginTransmission(address); |
|
|
130 |
Wire.send(0x01); // sends address of LSB reagister |
|
|
131 |
Wire.endTransmission(); // stop transmitting |
|
|
132 |
|
|
|
133 |
// Connect to device and request one byte |
|
|
134 |
Wire.beginTransmission(address); |
|
|
135 |
Wire.requestFrom(address, 1); |
|
|
136 |
ret = Wire.receive(); |
|
|
137 |
Wire.endTransmission(); // stop transmitting |
|
|
138 |
|
|
|
139 |
// Connect to device and set register address |
|
|
140 |
Wire.beginTransmission(address); |
|
|
141 |
Wire.send(0x02); // sends address of MSB register |
|
|
142 |
Wire.endTransmission(); // stop transmitting |
|
|
143 |
|
|
|
144 |
// Connect to device and request one byte |
|
|
145 |
Wire.beginTransmission(address); |
|
|
146 |
Wire.requestFrom(address, 1); |
|
|
147 |
ret +=256 * Wire.receive(); |
|
|
148 |
Wire.endTransmission(); // stop transmitting |
|
|
149 |
|
|
|
150 |
return (1000.0/pow(2.0,16)*ret); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
void loop() |
|
|
154 |
{ |
|
|
155 |
float luxVIS=0; |
|
|
156 |
float luxIR=0; |
2243 |
kaklik |
157 |
float time; |
2241 |
kaklik |
158 |
|
|
|
159 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
|
|
160 |
led_blink(); // Delay for measurement |
|
|
161 |
luxVIS=get_light_measurement(); |
|
|
162 |
|
2244 |
kaklik |
163 |
time=millis()/1000.0; |
2243 |
kaklik |
164 |
|
2241 |
kaklik |
165 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
|
|
166 |
led_blink(); // Delay for measurement |
|
|
167 |
luxIR=get_light_measurement(); |
|
|
168 |
|
|
|
169 |
// open the file. note that only one file can be open at a time, |
|
|
170 |
// so you have to close this one before opening another. |
2244 |
kaklik |
171 |
File dataFile = SD.open(filename, FILE_WRITE); |
2241 |
kaklik |
172 |
|
|
|
173 |
// if the file is available, write to it: |
|
|
174 |
if (dataFile) { |
2243 |
kaklik |
175 |
dataFile.print("$LUX0.1 "); |
|
|
176 |
dataFile.print(time,3); |
|
|
177 |
dataFile.print(" "); |
|
|
178 |
dataFile.print(luxVIS,3); |
|
|
179 |
dataFile.print(" "); |
|
|
180 |
dataFile.println(luxIR,3); |
2241 |
kaklik |
181 |
dataFile.close(); |
2243 |
kaklik |
182 |
|
2241 |
kaklik |
183 |
// print to the serial port too: |
2243 |
kaklik |
184 |
Serial.print("$LUX0.1 "); |
|
|
185 |
Serial.print(time,3); |
|
|
186 |
Serial.print(" "); |
2242 |
kaklik |
187 |
Serial.print(luxVIS,3); |
|
|
188 |
Serial.print(" "); |
|
|
189 |
Serial.println(luxIR,3); |
2241 |
kaklik |
190 |
} |
|
|
191 |
// if the file isn't open, pop up an error: |
|
|
192 |
else { |
2244 |
kaklik |
193 |
Serial.print("error opening "); |
|
|
194 |
Serial.println(filename); |
2241 |
kaklik |
195 |
} |
|
|
196 |
} |
|
|
197 |
|