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