]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/SD/examples/Datalogger/Datalogger.pde
first commit
[arduino] / books / pdummies / Libraries / SD / examples / Datalogger / Datalogger.pde
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  * SD card attached to SPI bus as follows:
9  ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
10   and pin #10 (SS) must be an output
11  ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
12   and pin #52 (SS) must be an output
13  ** Leonardo: Connect to hardware SPI via the ICSP header
14                 Pin 4 used here for consistency with other Arduino examples
15  
16  created  24 Nov 2010
17  modified 9 Apr 2012 by Tom Igoe
18  
19  This example code is in the public domain.
20          
21  */
22
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;
30
31 File dataFile;
32
33 void setup()
34 {
35  // Open serial communications and wait for port to open:
36   Serial.begin(9600);
37    while (!Serial) {
38     ; // wait for serial port to connect. Needed for Leonardo only
39   }
40
41
42   Serial.print("Initializing SD card...");
43   // make sure that the default chip select pin is set to
44   // output, even if you don't use it:
45   pinMode(SS, OUTPUT);
46   
47   // see if the card is present and can be initialized:
48   if (!SD.begin(chipSelect)) {
49     Serial.println("Card failed, or not present");
50     // don't do anything more:
51     while (1) ;
52   }
53   Serial.println("card initialized.");
54   
55   // Open up the file we're going to log to!
56   dataFile = SD.open("datalog.txt", FILE_WRITE);
57   if (! dataFile) {
58     Serial.println("error opening datalog.txt");
59     // Wait forever since we cant write data
60     while (1) ;
61   }
62 }
63
64 void loop()
65 {
66   // make a string for assembling the data to log:
67   String dataString = "";
68
69   // read three sensors and append to the string:
70   for (int analogPin = 0; analogPin < 3; analogPin++) {
71     int sensor = analogRead(analogPin);
72     dataString += String(sensor);
73     if (analogPin < 2) {
74       dataString += ","; 
75     }
76   }
77
78   dataFile.println(dataString);
79
80   // print to the serial port too:
81   Serial.println(dataString);
82   
83   // The following line will 'save' the file to the SD card after every
84   // line of data - this will use more power and slow down how much data
85   // you can read but it's safer! 
86   // If you want to speed up the system, remove the call to flush() and it
87   // will save the file only every 512 bytes - every time a sector on the 
88   // SD card is filled with data.
89   dataFile.flush();
90   
91   // Take 1 measurement every 500 milliseconds
92   delay(500);
93 }
94
95
96
97
98
99
100
101
102