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