Rev 2243 Rev 2244
1 /* I2C Light Sensor 1 /* I2C Light Sensor
2   2  
3 The circuit: 3 The circuit:
4 * analog sensors on analog ins PC0, PC1, and PC2 4 * analog sensors on analog ins PC0, PC1, and PC2
5 * SD card attached to SPI bus as follows: 5 * SD card attached to SPI bus as follows:
6 ** MOSI/CMD - PB3 6 ** MOSI/CMD - PB3
7 ** MISO/DAT0 - PB4 7 ** MISO/DAT0 - PB4
8 ** CLK - PB5 8 ** CLK - PB5
9 ** CS/CD/DAT3 - PD4 (4) 9 ** CS/CD/DAT3 - PD4 (4)
10   10  
11 I2C pins PC4, PC5 11 I2C pins PC4, PC5
12 12
13 */ 13 */
14   14  
15 #include <Wire.h> 15 #include <Wire.h>
16 #include <math.h> 16 #include <math.h>
17 #include <stdlib.h> 17 #include <stdlib.h>
18 #include <SD.h> 18 #include <SD.h>
19   19  
20 #define chipSelect 4 20 #define chipSelect 4
21   21  
22   22  
23 #define address 0x44 // A0 = L 23 #define address 0x44 // A0 = L
24 #define SENSE_VIS 0 24 #define SENSE_VIS 0
25 #define SENSE_IR 1 25 #define SENSE_IR 1
26   26  
27 #define LIGHT_AUTORANGE 0 27 #define LIGHT_AUTORANGE 0
28 #define LIGHT_RANGE1 1 28 #define LIGHT_RANGE1 1
29 #define LIGHT_RANGE2 2 29 #define LIGHT_RANGE2 2
30 #define LIGHT_RANGE3 3 30 #define LIGHT_RANGE3 3
31 #define LIGHT_RANGE4 4 31 #define LIGHT_RANGE4 4
32   32  
-   33 char filename[13];
-   34 int data = 0;
-   35 int light_sensor_setup;
-   36  
33 void setup() 37 void setup()
34 { 38 {
-   39 int count=0;
-   40  
35 Wire.begin(); // join i2c bus (address optional for master) 41 Wire.begin(); // join i2c bus (address optional for master)
36   42  
37 pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela 43 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 44 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 45 Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku
40 46
41 Serial.print("Initializing SD card..."); 47 Serial.print("Initializing SD card...");
42 // make sure that the default chip select pin is set to 48 // make sure that the default chip select pin is set to
43 // output, even if you don't use it: 49 // output, even if you don't use it:
44 pinMode(10, OUTPUT); 50 pinMode(10, OUTPUT);
45 51
46 // see if the card is present and can be initialized: 52 // see if the card is present and can be initialized:
47 if (!SD.begin(chipSelect)) { 53 if (!SD.begin(chipSelect)) {
48 Serial.println("Card failed, or not present"); 54 Serial.println("Card failed, or not present");
49 // don't do anything more: 55 // don't do anything more:
50 return; 56 return;
51 } 57 }
52 Serial.println("card initialized."); 58 Serial.println("card initialized.");
-   59 Serial.print("searching for files...");
-   60
53 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);
54 } 69 }
55   70  
56 int data = 0; -  
57 int light_sensor_setup; -  
58   -  
59 void led_blink() 71 void led_blink()
60 { 72 {
61 digitalWrite(3, HIGH); // set the LED on 73 digitalWrite(3, HIGH); // set the LED on
62 delay(500); 74 delay(500);
63 digitalWrite(3, LOW); // set the LED off 75 digitalWrite(3, LOW); // set the LED off
64 delay(500); 76 delay(500);
65 } 77 }
66   78  
67   -  
68 int set_light_sensor(int mode) 79 int set_light_sensor(int mode)
69 { 80 {
70 int command; 81 int command;
71 82
72 switch (mode) 83 switch (mode)
73 { 84 {
74 case SENSE_VIS: 85 case SENSE_VIS:
75 { 86 {
76 command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) 87 command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1)
77 break; 88 break;
78 } 89 }
79   90  
80 case SENSE_IR: 91 case SENSE_IR:
81 { 92 {
82 command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) 93 command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx])
83 break; 94 break;
84 } 95 }
85 96
86 default: 97 default:
87 return 3; 98 return 3;
88 } 99 }
89   100  
90 // Setup device 101 // Setup device
91 Wire.beginTransmission(address); 102 Wire.beginTransmission(address);
92 Wire.send(0x00); // sends address 103 Wire.send(0x00); // sends address
93 Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) 104 Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1)
94 Wire.endTransmission(); // stop transmitting 105 Wire.endTransmission(); // stop transmitting
95 106
96 107
97 // Connect to device and set register address 108 // Connect to device and set register address
98 Wire.beginTransmission(address); 109 Wire.beginTransmission(address);
99 Wire.send(0x00); // sends address (command register) 110 Wire.send(0x00); // sends address (command register)
100 Wire.endTransmission(); // stop transmitting 111 Wire.endTransmission(); // stop transmitting
101 112
102 // verify written command byte 113 // verify written command byte
103 Wire.beginTransmission(address); 114 Wire.beginTransmission(address);
104 Wire.requestFrom(address, 1); 115 Wire.requestFrom(address, 1);
105 if (command != Wire.receive()) 116 if (command != Wire.receive())
106 { 117 {
107 return 4; 118 return 4;
108 Serial.print(data, BIN); 119 Serial.print(data, BIN);
109 } 120 }
110 Wire.endTransmission(); // stop transmitting 121 Wire.endTransmission(); // stop transmitting
111 light_sensor_setup=command; 122 light_sensor_setup=command;
112 } 123 }
113   124  
114 float get_light_measurement() 125 float get_light_measurement()
115 { 126 {
116 int ret=0; 127 int ret=0;
117   128  
118 // Connect to device and set register address 129 // Connect to device and set register address
119 Wire.beginTransmission(address); 130 Wire.beginTransmission(address);
120 Wire.send(0x01); // sends address of LSB reagister 131 Wire.send(0x01); // sends address of LSB reagister
121 Wire.endTransmission(); // stop transmitting 132 Wire.endTransmission(); // stop transmitting
122 133
123 // Connect to device and request one byte 134 // Connect to device and request one byte
124 Wire.beginTransmission(address); 135 Wire.beginTransmission(address);
125 Wire.requestFrom(address, 1); 136 Wire.requestFrom(address, 1);
126 ret = Wire.receive(); 137 ret = Wire.receive();
127 Wire.endTransmission(); // stop transmitting 138 Wire.endTransmission(); // stop transmitting
128 139
129 // Connect to device and set register address 140 // Connect to device and set register address
130 Wire.beginTransmission(address); 141 Wire.beginTransmission(address);
131 Wire.send(0x02); // sends address of MSB register 142 Wire.send(0x02); // sends address of MSB register
132 Wire.endTransmission(); // stop transmitting 143 Wire.endTransmission(); // stop transmitting
133 144
134 // Connect to device and request one byte 145 // Connect to device and request one byte
135 Wire.beginTransmission(address); 146 Wire.beginTransmission(address);
136 Wire.requestFrom(address, 1); 147 Wire.requestFrom(address, 1);
137 ret +=256 * Wire.receive(); 148 ret +=256 * Wire.receive();
138 Wire.endTransmission(); // stop transmitting 149 Wire.endTransmission(); // stop transmitting
139   150  
140 return (1000.0/pow(2.0,16)*ret); 151 return (1000.0/pow(2.0,16)*ret);
141 } 152 }
142   153  
143 char *ftoa(char *a, double f, int precision) 154 char *ftoa(char *a, double f, int precision)
144 { 155 {
145 long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; 156 long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
146 157
147 char *ret = a; 158 char *ret = a;
148 long heiltal = (long)f; 159 long heiltal = (long)f;
149 itoa(heiltal, a, 10); 160 itoa(heiltal, a, 10);
150 while (*a != '\0') a++; 161 while (*a != '\0') a++;
151 *a++ = '.'; 162 *a++ = '.';
152 long desimal = abs((long)((f - heiltal) * p[precision])); 163 long desimal = abs((long)((f - heiltal) * p[precision]));
153 itoa(desimal, a, 10); 164 itoa(desimal, a, 10);
154 return ret; 165 return ret;
155 } 166 }
156   167  
157 void loop() 168 void loop()
158 { 169 {
159 float luxVIS=0; 170 float luxVIS=0;
160 float luxIR=0; 171 float luxIR=0;
161 float time; 172 float time;
162   173  
163 set_light_sensor(SENSE_VIS); //setup sensor for visible measuring 174 set_light_sensor(SENSE_VIS); //setup sensor for visible measuring
164 led_blink(); // Delay for measurement 175 led_blink(); // Delay for measurement
165 luxVIS=get_light_measurement(); 176 luxVIS=get_light_measurement();
166   177  
167 time=millis()/1000; 178 time=millis()/1000.0;
168 179
169 set_light_sensor(SENSE_IR); // setup sensor for infrared measuring 180 set_light_sensor(SENSE_IR); // setup sensor for infrared measuring
170 led_blink(); // Delay for measurement 181 led_blink(); // Delay for measurement
171 luxIR=get_light_measurement(); 182 luxIR=get_light_measurement();
172 183
173 // open the file. note that only one file can be open at a time, 184 // 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. 185 // so you have to close this one before opening another.
175 File dataFile = SD.open("datalog.txt", FILE_WRITE); 186 File dataFile = SD.open(filename, FILE_WRITE);
176   187  
177 // if the file is available, write to it: 188 // if the file is available, write to it:
178 if (dataFile) { 189 if (dataFile) {
179 dataFile.print("$LUX0.1 "); 190 dataFile.print("$LUX0.1 ");
180 dataFile.print(time,3); 191 dataFile.print(time,3);
181 dataFile.print(" "); 192 dataFile.print(" ");
182 dataFile.print(luxVIS,3); 193 dataFile.print(luxVIS,3);
183 dataFile.print(" "); 194 dataFile.print(" ");
184 dataFile.println(luxIR,3); 195 dataFile.println(luxIR,3);
185 dataFile.close(); 196 dataFile.close();
186 197
187 // print to the serial port too: 198 // print to the serial port too:
188 Serial.print("$LUX0.1 "); 199 Serial.print("$LUX0.1 ");
189 Serial.print(time,3); 200 Serial.print(time,3);
190 Serial.print(" "); 201 Serial.print(" ");
191 Serial.print(luxVIS,3); 202 Serial.print(luxVIS,3);
192 Serial.print(" "); 203 Serial.print(" ");
193 Serial.println(luxIR,3); 204 Serial.println(luxIR,3);
194 } 205 }
195 // if the file isn't open, pop up an error: 206 // if the file isn't open, pop up an error:
196 else { 207 else {
197 Serial.println("error opening datalog.txt"); 208 Serial.print("error opening ");
-   209 Serial.println(filename);
198 } 210 }
199 } 211 }
200   212