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