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