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