Rev 2195 Rev 2201
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 PC0, PC1, and PC2
9 * SD card attached to SPI bus as follows: 9 * SD card attached to SPI bus as follows:
10 ** MOSI - pin 11 10 ** MOSI - PB3
11 ** MISO - pin 12 11 ** MISO - PB4
12 ** CLK - pin 13 12 ** CLK - PB5
13 ** CS - pin 4 13 ** CS - PD4
14 14
15 created 24 Nov 2010 15 created 24 Nov 2010
16 updated 2 Dec 2010 16 updated 2 Dec 2010
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   -  
25 // On the Ethernet Shield, CS is pin 4. Note that even if it's not -  
26 // used as the CS pin, the hardware CS pin (10 on most Arduino boards, -  
27 // 53 on the Mega) must be left as an output or the SD library -  
28 // functions will not work. -  
29 const int chipSelect = 4; 24 const int chipSelect = 4;
30   25  
31 void setup() 26 void setup()
32 { 27 {
33 Serial.begin(9600); 28 Serial.begin(9600);
34 Serial.print("Initializing SD card..."); 29 Serial.print("Initializing SD card...");
35 // make sure that the default chip select pin is set to 30 // make sure that the default chip select pin is set to
36 // output, even if you don't use it: 31 // output, even if you don't use it:
37 pinMode(10, OUTPUT); 32 pinMode(10, OUTPUT);
38 33
39 // see if the card is present and can be initialized: 34 // see if the card is present and can be initialized:
40 if (!SD.begin(chipSelect)) { 35 if (!SD.begin(chipSelect)) {
41 Serial.println("Card failed, or not present"); 36 Serial.println("Card failed, or not present");
42 // don't do anything more: 37 // don't do anything more:
43 return; 38 return;
44 } 39 }
45 Serial.println("card initialized."); 40 Serial.println("card initialized.");
46 } 41 }
47   42  
-   43 int count;
-   44  
48 void loop() 45 void loop()
49 { 46 {
-   47  
50 // make a string for assembling the data to log: 48 // make a string for assembling the data to log:
51 String dataString = ""; 49 String dataString = "$";
-   50 delay(100);
-   51 dataString += String(count); // print number of measurement
-   52 dataString += ",";
-   53  
52   54  
53 // read three sensors and append to the string: 55 // read three sensors and append to the string:
54 for (int analogPin = 0; analogPin < 3; analogPin++) { 56 for (int analogPin = 0; analogPin < 3; analogPin++) {
55 int sensor = analogRead(analogPin); 57 int sensor = analogRead(analogPin);
56 dataString += String(sensor); 58 dataString += String(sensor);
57 if (analogPin < 2) { 59 if (analogPin < 2) {
58 dataString += ","; 60 dataString += ",";
59 } 61 }
60 } 62 }
61   63  
62 // open the file. note that only one file can be open at a time, 64 // open the file. note that only one file can be open at a time,
63 // so you have to close this one before opening another. 65 // so you have to close this one before opening another.
64 File dataFile = SD.open("datalog.txt", FILE_WRITE); 66 File dataFile = SD.open("datalog.txt", FILE_WRITE);
65   67  
66 // if the file is available, write to it: 68 // if the file is available, write to it:
67 if (dataFile) { 69 if (dataFile) {
68 dataFile.println(dataString); 70 dataFile.println(dataString);
69 dataFile.close(); 71 dataFile.close();
70 // print to the serial port too: 72 // print to the serial port too:
71 Serial.println(dataString); 73 Serial.println(dataString);
-   74 count++;
72 } 75 }
73 // if the file isn't open, pop up an error: 76 // if the file isn't open, pop up an error:
74 else { 77 else {
75 Serial.println("error opening datalog.txt"); 78 Serial.println("error opening datalog.txt");
76 } 79 }
77 } -  
78 80 }
-   81