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
9 ** CS/CD/DAT3 - PB2 (10)
10  
11 */
12  
13 #include <Wire.h>
14 #include <math.h>
15 #include <SD.h>
16  
17 #define chipSelect 10
18  
19  
20 #define address 0x44 // A0 = L
21 #define SENSE_VIS 0
22 #define SENSE_IR 1
23  
24 #define LIGHT_AUTORANGE 0
25 #define LIGHT_RANGE1 1
26 #define LIGHT_RANGE2 2
27 #define LIGHT_RANGE3 3
28 #define LIGHT_RANGE4 4
29  
30 int count=0;
31  
32 void setup()
33 {
34 Wire.begin(); // join i2c bus (address optional for master)
35 // I2C pins PD4, PD5
36 //
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  
143 void loop()
144 {
145 float luxVIS=0;
146 float luxIR=0;
147  
148 set_light_sensor(SENSE_VIS); //setup sensor for visible measuring
149 led_blink(); // Delay for measurement
150 luxVIS=get_light_measurement();
151  
152 set_light_sensor(SENSE_IR); // setup sensor for infrared measuring
153 led_blink(); // Delay for measurement
154 luxIR=get_light_measurement();
155  
156 // make a string for assembling the data to log:
157 String dataString = "$LOG";
158 dataString += String((int)count); // print number of measurement
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  
164 // 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.
166 File dataFile = SD.open("datalog.txt", FILE_WRITE);
167  
168 // if the file is available, write to it:
169 if (dataFile) {
170 dataFile.println(dataString);
171 dataFile.close();
172 // print to the serial port too:
173 Serial.println(dataString);
174 count++;
175 }
176 // if the file isn't open, pop up an error:
177 else {
178 Serial.println("error opening datalog.txt");
179 }
180 }
181