3885 |
kakl |
1 |
/* |
|
|
2 |
SD card datalogger |
|
|
3 |
|
|
|
4 |
This example shows how to log data from three analog sensors |
|
|
5 |
to an SD card using the SD library. |
|
|
6 |
|
|
|
7 |
The circuit: |
|
|
8 |
* analog sensors on analog ins 0, 1, and 2 |
|
|
9 |
* SD card attached to SPI bus as follows: |
|
|
10 |
** MOSI - pin 11 |
|
|
11 |
** MISO - pin 12 |
|
|
12 |
** CLK - pin 13 |
|
|
13 |
** CS - pin 4 |
|
|
14 |
|
|
|
15 |
created 24 Nov 2010 |
|
|
16 |
modified 9 Apr 2012 |
|
|
17 |
by Tom Igoe |
|
|
18 |
|
|
|
19 |
This example code is in the public domain. |
|
|
20 |
|
|
|
21 |
*/ |
|
|
22 |
|
|
|
23 |
#include <SD.h> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
// On the Ethernet Shield, CS is pin 4. Note that even if it's not |
|
|
27 |
// used as the CS pin, the hardware CS pin (10 on most Arduino boards, |
|
|
28 |
// 53 on the Mega) must be left as an output or the SD library |
|
|
29 |
// functions will not work. |
|
|
30 |
const int chipSelect = 4; |
|
|
31 |
int led = 5; |
|
|
32 |
unsigned int n = 0; |
|
|
33 |
char inChar; |
|
|
34 |
String dataString = ""; |
|
|
35 |
String filename = "log.csv"; |
|
|
36 |
int coll = 0; |
|
|
37 |
|
|
|
38 |
void setup() |
|
|
39 |
{ |
|
|
40 |
// Open serial communications and wait for port to open: |
|
|
41 |
Serial.begin(9600); |
|
|
42 |
while (!Serial) { |
|
|
43 |
; // wait for serial port to connect. Needed for Leonardo only |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
Serial.print("Initializing SD card..."); |
|
|
48 |
// make sure that the default chip select pin is set to |
|
|
49 |
// output, even if you don't use it: |
|
|
50 |
pinMode(10, OUTPUT); |
|
|
51 |
pinMode(led, OUTPUT); |
|
|
52 |
|
|
|
53 |
// see if the card is present and can be initialized: |
|
|
54 |
if (!SD.begin(chipSelect)) { |
|
|
55 |
Serial.println("Card failed, or not present"); |
|
|
56 |
// don't do anything more: |
|
|
57 |
return; |
|
|
58 |
} |
|
|
59 |
Serial.println("card initialized."); |
|
|
60 |
|
|
|
61 |
ReadGPS(); |
|
|
62 |
// filename = dataString.substring(0,5) + dataString.substring(dataString.length()-5,dataString.length()) + filename; |
|
|
63 |
int len = dataString.length()-1; |
|
|
64 |
filename = dataString.substring(len-2,len) + dataString.substring(len-4,len-2) + dataString.substring(len-6,len-4) + dataString.substring(0,2) + ".CSV "; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
void loop() |
|
|
68 |
{ |
|
|
69 |
ReadGPS(); |
|
|
70 |
|
|
|
71 |
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) |
|
|
72 |
delay(20); // wait for a second |
|
|
73 |
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW |
|
|
74 |
delay(20); // wait for a second |
|
|
75 |
|
|
|
76 |
// make a string for assembling the data to log: |
|
|
77 |
// String dataString = ""; |
|
|
78 |
|
|
|
79 |
dataString += String(n++); |
|
|
80 |
dataString += ","; |
|
|
81 |
|
|
|
82 |
// read three sensors and append to the string: |
|
|
83 |
for (int analogPin = 0; analogPin < 2; analogPin++) |
|
|
84 |
{ |
|
|
85 |
int sensor = analogRead(analogPin); |
|
|
86 |
dataString += String(sensor); |
|
|
87 |
if (analogPin < 1) |
|
|
88 |
{ |
|
|
89 |
dataString += ","; |
|
|
90 |
} |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
char fileNameCharArray[filename.length()]; |
|
|
94 |
filename.toCharArray(fileNameCharArray, filename.length()); |
|
|
95 |
File dataFile = SD.open(fileNameCharArray, FILE_WRITE); |
|
|
96 |
// open the file. note that only one file can be open at a time, |
|
|
97 |
// so you have to close this one before opening another. |
|
|
98 |
//File dataFile = SD.open(filename, FILE_WRITE); |
|
|
99 |
|
|
|
100 |
// if the file is available, write to it: |
|
|
101 |
if (dataFile) { |
|
|
102 |
dataFile.println(dataString); |
|
|
103 |
dataFile.close(); |
|
|
104 |
// print to the serial port too: |
|
|
105 |
Serial.println(dataString); |
|
|
106 |
} |
|
|
107 |
// if the file isn't open, pop up an error: |
|
|
108 |
else { |
|
|
109 |
Serial.println("error opening datalog.txt"); |
|
|
110 |
} |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
|
|
|
114 |
void ReadGPS() |
|
|
115 |
{ |
|
|
116 |
dataString = ""; |
|
|
117 |
coll = 0; |
|
|
118 |
|
|
|
119 |
while(1) |
|
|
120 |
{ |
|
|
121 |
// get incoming byte: |
|
|
122 |
while (!Serial.available()); |
|
|
123 |
if (Serial.read() != '$') continue; |
|
|
124 |
while (!Serial.available()); |
|
|
125 |
if (Serial.read() != 'G') continue; |
|
|
126 |
while (!Serial.available()); |
|
|
127 |
if (Serial.read() != 'P') continue; |
|
|
128 |
while (!Serial.available()); |
|
|
129 |
if (Serial.read() != 'R') continue; |
|
|
130 |
while (!Serial.available()); |
|
|
131 |
if (Serial.read() != 'M') continue; |
|
|
132 |
while (!Serial.available()); |
|
|
133 |
if (Serial.read() != 'C') continue; |
|
|
134 |
while (!Serial.available()); |
|
|
135 |
if (Serial.read() != ',') continue; |
|
|
136 |
break; |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
do |
|
|
140 |
{ |
|
|
141 |
while (!Serial.available()); |
|
|
142 |
inChar = (char)Serial.read(); |
|
|
143 |
if (inChar == ',') coll++; |
|
|
144 |
dataString += inChar; |
|
|
145 |
} |
|
|
146 |
while (coll < 9); |
|
|
147 |
} |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|