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