Line 4... |
Line 4... |
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 - PB2 (10) |
9 |
** CS/CD/DAT3 - PD4 (4) |
- |
|
10 |
|
- |
|
11 |
I2C pins PC4, PC5 |
10 |
|
12 |
|
11 |
*/ |
13 |
*/ |
12 |
|
14 |
|
13 |
#include <Wire.h> |
15 |
#include <Wire.h> |
14 |
#include <math.h> |
16 |
#include <math.h> |
- |
|
17 |
#include <stdlib.h> |
15 |
#include <SD.h> |
18 |
#include <SD.h> |
16 |
|
19 |
|
17 |
#define chipSelect 10 |
20 |
#define chipSelect 4 |
18 |
|
21 |
|
19 |
|
22 |
|
20 |
#define address 0x44 // A0 = L |
23 |
#define address 0x44 // A0 = L |
21 |
#define SENSE_VIS 0 |
24 |
#define SENSE_VIS 0 |
22 |
#define SENSE_IR 1 |
25 |
#define SENSE_IR 1 |
Line 30... |
Line 33... |
30 |
int count=0; |
33 |
int count=0; |
31 |
|
34 |
|
32 |
void setup() |
35 |
void setup() |
33 |
{ |
36 |
{ |
34 |
Wire.begin(); // join i2c bus (address optional for master) |
37 |
Wire.begin(); // join i2c bus (address optional for master) |
35 |
// I2C pins PD4, PD5 |
- |
|
36 |
// |
38 |
|
37 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
39 |
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 |
40 |
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 |
41 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
40 |
|
42 |
|
41 |
Serial.print("Initializing SD card..."); |
43 |
Serial.print("Initializing SD card..."); |
Line 138... |
Line 140... |
138 |
Wire.endTransmission(); // stop transmitting |
140 |
Wire.endTransmission(); // stop transmitting |
139 |
|
141 |
|
140 |
return (1000.0/pow(2.0,16)*ret); |
142 |
return (1000.0/pow(2.0,16)*ret); |
141 |
} |
143 |
} |
142 |
|
144 |
|
- |
|
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 |
|
143 |
void loop() |
159 |
void loop() |
144 |
{ |
160 |
{ |
145 |
float luxVIS=0; |
161 |
float luxVIS=0; |
146 |
float luxIR=0; |
162 |
float luxIR=0; |
147 |
|
163 |
|
- |
|
164 |
char dataString[40]; |
- |
|
165 |
char luxVISch[10]; |
- |
|
166 |
char luxIRch[10]; |
- |
|
167 |
|
148 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
168 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
149 |
led_blink(); // Delay for measurement |
169 |
led_blink(); // Delay for measurement |
150 |
luxVIS=get_light_measurement(); |
170 |
luxVIS=get_light_measurement(); |
151 |
|
171 |
|
152 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
172 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
153 |
led_blink(); // Delay for measurement |
173 |
led_blink(); // Delay for measurement |
154 |
luxIR=get_light_measurement(); |
174 |
luxIR=get_light_measurement(); |
155 |
|
175 |
|
- |
|
176 |
ftoa(luxVISch,luxVIS,2); |
- |
|
177 |
ftoa(luxIRch,luxIR,2); |
156 |
// make a string for assembling the data to log: |
178 |
// make a string for assembling the data to log: |
157 |
String dataString = "$LOG"; |
- |
|
158 |
dataString += String((int)count); // print number of measurement |
179 |
sprintf(dataString,"$LUX01 %d %s %s",count, luxVISch, luxIRch); |
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 |
|
180 |
|
164 |
// open the file. note that only one file can be open at a time, |
181 |
// 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. |
182 |
// so you have to close this one before opening another. |
166 |
File dataFile = SD.open("datalog.txt", FILE_WRITE); |
183 |
File dataFile = SD.open("datalog.txt", FILE_WRITE); |
167 |
|
184 |
|
168 |
// if the file is available, write to it: |
185 |
// if the file is available, write to it: |
169 |
if (dataFile) { |
186 |
if (dataFile) { |
170 |
dataFile.println(dataString); |
187 |
dataFile.println(dataString); |
171 |
dataFile.close(); |
188 |
dataFile.close(); |
172 |
// print to the serial port too: |
189 |
// print to the serial port too: |
- |
|
190 |
Serial.print(luxVIS,3); |
- |
|
191 |
Serial.print(" "); |
- |
|
192 |
Serial.println(luxIR,3); |
173 |
Serial.println(dataString); |
193 |
Serial.println(dataString); |
174 |
count++; |
194 |
count++; |
175 |
} |
195 |
} |
176 |
// if the file isn't open, pop up an error: |
196 |
// if the file isn't open, pop up an error: |
177 |
else { |
197 |
else { |