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