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