Rev 3886 Rev 3888
Line 1... Line 1...
1 /* 1 /*
2 SD card datalogger 2 SD card datalogger GPSRL04A
3 -  
4 This example shows how to log data from three analog sensors -  
5 to an SD card using the SD library. -  
6 3
7 The circuit: 4 The circuit:
8 * analog sensors on analog ins 0, 1, and 2 5 * analog sensors are connected to analog ins PC0, PC1
9 * SD card attached to SPI bus as follows: 6 * SD card attached to SPI bus as follows:
10 ** MOSI - pin 11 7 ** MOSI - pin PB3
11 ** MISO - pin 12 8 ** MISO - pin PB4
12 ** CLK - pin 13 9 ** CLK - pin PB5
13 ** CS - pin 4 10 ** CS - pin PD4
14 -  
15 created 24 Nov 2010 -  
16 modified 9 Apr 2012 -  
17 by Tom Igoe -  
18 11
19 This example code is in the public domain. -  
20 -  
21 */ 12 */
22   13  
23 #include <SD.h> 14 #include <SD.h>
24   15  
25   -  
26 // On the Ethernet Shield, CS is pin 4. Note that even if it's not 16 const int chipSelect = 4; // CS is PD4
27 // used as the CS pin, the hardware CS pin (10 on most Arduino boards, 17 int LED = 5; // LED is conected from PD5 to LED at GPS module
28 // 53 on the Mega) must be left as an output or the SD library 18 unsigned int n = 0; // measurement counter
29 // functions will not work. -  
30 const int chipSelect = 4; 19 char inChar; // input character from GPS
31 int led = 5; -  
32 unsigned int n = 0; -  
33 char inChar; -  
34 String dataString = ""; 20 String dataString = ""; // concantenated string with NMEA messages and measured values
35 String filename = "log.csv"; 21 String filename = "log.csv"; // generated filename for log file
36 int coll = 0; 22 int coll = 0; // collons counter in NMEA messages
37   23  
38 void setup() 24 void setup()
39 { 25 {
40 // Open serial communications and wait for port to open: 26 // Open serial communications and wait for port to open:
41 Serial.begin(9600); 27 Serial.begin(9600);
42 while (!Serial) { 28 while (!Serial) {;}
43 ; // wait for serial port to connect. Needed for Leonardo only -  
44 } -  
45   29  
46   -  
47 Serial.print("Initializing SD card..."); 30 Serial.print("Initializing SD card..."); // inserting a SD Card always reset the processor and call setup
48 // make sure that the default chip select pin is set to 31 // make sure that the default chip select pin is set to
49 // output, even if you don't use it: 32 // output, even if you don't use it:
50 pinMode(10, OUTPUT); 33 pinMode(10, OUTPUT); // PB2
51 pinMode(led, OUTPUT); 34 pinMode(LED, OUTPUT);
52   35  
53 // see if the card is present and can be initialized: 36 // see if the card is present and can be initialized:
54 if (!SD.begin(chipSelect)) { 37 if (!SD.begin(chipSelect))
-   38 {
55 Serial.println("Card failed, or not present"); 39 Serial.println("Card failed, or not present");
56 // don't do anything more: 40 // don't do anything more:
57 return; 41 return;
58 } 42 }
59 Serial.println("card initialized."); 43 Serial.println("card initialized.");
60   44  
61 dataString = ""; 45 dataString = "";
62 ReadGPRMC(); 46 ReadGPRMC(); // read time from GPS
63   -  
64 // filename = dataString.substring(0,5) + dataString.substring(dataString.length()-5,dataString.length()) + filename; 47 // derive file name fom time (YYMMDDHH.CSV)
65 int len = dataString.length()-1; 48 int len = dataString.length()-1;
66 filename = dataString.substring(len-2,len) + dataString.substring(len-4,len-2) + dataString.substring(len-6,len-4) + dataString.substring(0,2) + ".CSV "; 49 filename = dataString.substring(len-2,len) + dataString.substring(len-4,len-2) + dataString.substring(len-6,len-4) + dataString.substring(0,2) + ".CSV ";
67 } 50 }
68   51  
69 void loop() 52 void loop()
70 { 53 {
71 dataString = ""; 54 dataString = ""; // make a string for assembling the data to log
72 ReadGPRMC(); 55 ReadGPRMC(); // read NMEA sentences from GPS
73 ReadGPGGA(); 56 ReadGPGGA();
74 57
75 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 58 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
76 delay(20); // wait for a second 59 delay(20); // wait for a while
77 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 60 digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
78 delay(20); // wait for a second 61 delay(20); // wait for a while
79   62  
80 // make a string for assembling the data to log: 63 // make a string for assembling the data to log:
81 // String dataString = ""; -  
82 -  
83 dataString += String(n++); 64 dataString += String(n++);
84 dataString += ","; 65 dataString += ",";
85   66  
86 // read three sensors and append to the string: 67 // read analog values from sensors and append to the string:
87 for (int analogPin = 0; analogPin < 2; analogPin++) 68 for (int analogPin = 0; analogPin < 2; analogPin++)
88 { 69 {
89 int sensor = analogRead(analogPin); 70 int sensor = analogRead(analogPin);
90 dataString += String(sensor); 71 dataString += String(sensor);
91 if (analogPin < 1) 72 if (analogPin < 1) // suppress last coma
92 { 73 {
93 dataString += ","; 74 dataString += ",";
94 } 75 }
95 } 76 }
96   77  
-   78 // open the file. note that only one file can be open at a time,
-   79 // so you have to close this one before opening another.
97 char fileNameCharArray[filename.length()]; 80 char fileNameCharArray[filename.length()];
98 filename.toCharArray(fileNameCharArray, filename.length()); 81 filename.toCharArray(fileNameCharArray, filename.length());
99 File dataFile = SD.open(fileNameCharArray, FILE_WRITE); 82 File dataFile = SD.open(fileNameCharArray, FILE_WRITE);
100 // open the file. note that only one file can be open at a time, -  
101 // so you have to close this one before opening another. -  
102 //File dataFile = SD.open(filename, FILE_WRITE); -  
103   83  
104 // if the file is available, write to it: 84 // if the file is available, write to it:
105 if (dataFile) { 85 if (dataFile)
-   86 {
106 dataFile.println(dataString); 87 dataFile.println(dataString);
107 dataFile.close(); 88 dataFile.close();
108 // print to the serial port too: 89 // print to the serial port too:
109 Serial.println(dataString); 90 Serial.println(dataString);
110 } 91 }
111 // if the file isn't open, pop up an error: 92 // if the file isn't open, pop up an error:
112 else { 93 else {
113 Serial.println("error opening datalog.txt"); 94 Serial.println("error opening datalog.CSV");
114 } 95 }
115 } 96 }
116   97  
117   98  
-   99 // function for reading $GPRMC NMEA message
118 void ReadGPRMC() 100 void ReadGPRMC()
119 { 101 {
120 // $GPRMC,091451.00,A,4915.64143,N,01441.50397,E,0.053,,090215,,,A*74 102 // $GPRMC,091451.00,A,4915.64143,N,01441.50397,E,0.053,,090215,,,A*74
121   103  
122 coll = 0; 104 coll = 0;
123 105
124 while(1) 106 while(1) // wait for $GPRMC
125 { 107 {
126 // get incoming byte: 108 // get incoming byte:
127 while (!Serial.available()); 109 while (!Serial.available());
128 if (Serial.read() != '$') continue; 110 if (Serial.read() != '$') continue;
129 while (!Serial.available()); 111 while (!Serial.available());
Line 146... Line 128...
146 while (!Serial.available()); 128 while (!Serial.available());
147 inChar = (char)Serial.read(); 129 inChar = (char)Serial.read();
148 if (inChar == ',') coll++; 130 if (inChar == ',') coll++;
149 dataString += inChar; 131 dataString += inChar;
150 } 132 }
151 while (coll < 9); 133 while (coll < 9); // read only 9 coma separated values
152 } 134 }
153   135  
154   136  
-   137 // function for reading $GPGGA NMEA message
155 void ReadGPGGA() 138 void ReadGPGGA()
156 { 139 {
157 // $GPGGA,091451.00,4915.64143,N,01441.50397,E,1,09,0.90,443.2,M,44.0,M,,*50 140 // $GPGGA,091451.00,4915.64143,N,01441.50397,E,1,09,0.90,443.2,M,44.0,M,,*50
158   141  
159 coll = 0; 142 coll = 0;
160 143
161 while(1) 144 while(1) // wait for $GPGGA
162 { 145 {
163 // get incoming byte: -  
164 while (!Serial.available()); 146 while (!Serial.available());
165 if (Serial.read() != '$') continue; 147 if (Serial.read() != '$') continue;
166 while (!Serial.available()); 148 while (!Serial.available());
167 if (Serial.read() != 'G') continue; 149 if (Serial.read() != 'G') continue;
168 while (!Serial.available()); 150 while (!Serial.available());
Line 181... Line 163...
181 do 163 do
182 { 164 {
183 while (!Serial.available()); 165 while (!Serial.available());
184 inChar = (char)Serial.read(); 166 inChar = (char)Serial.read();
185 if (inChar == ',') coll++; 167 if (inChar == ',') coll++;
186 if (coll > 4) dataString += inChar; 168 if (coll > 4) dataString += inChar; // skip first 5 coma separated values
187 } 169 }
188 while (coll < 12); 170 while (coll < 12); // read only 7 coma separated values
189 } 171 }
190   172  
191   173  
192   174  
193   175