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