Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SD / examples / Datalogger / Datalogger.ino @ 58d82c77

History | View | Annotate | Download (1.9 KB)

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
 * analog sensors on analog ins 0, 1, and 2
9
 * SD card attached to SPI bus as follows:
10
 ** MOSI - pin 11
11
 ** MISO - pin 12
12
 ** CLK - pin 13
13
 ** CS - pin 4
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

    
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
void setup()
32
{
33
  Serial.begin(9600);
34
  Serial.print("Initializing SD card...");
35
  // make sure that the default chip select pin is set to
36
  // output, even if you don't use it:
37
  pinMode(10, OUTPUT);
38
  
39
  // see if the card is present and can be initialized:
40
  if (!SD.begin(chipSelect)) {
41
    Serial.println("Card failed, or not present");
42
    // don't do anything more:
43
    return;
44
  }
45
  Serial.println("card initialized.");
46
}
47

    
48
void loop()
49
{
50
  // make a string for assembling the data to log:
51
  String dataString = "";
52

    
53
  // read three sensors and append to the string:
54
  for (int analogPin = 0; analogPin < 3; analogPin++) {
55
    int sensor = analogRead(analogPin);
56
    dataString += String(sensor);
57
    if (analogPin < 2) {
58
      dataString += ","; 
59
    }
60
  }
61

    
62
  // 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.
64
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
65

    
66
  // if the file is available, write to it:
67
  if (dataFile) {
68
    dataFile.println(dataString);
69
    dataFile.close();
70
    // print to the serial port too:
71
    Serial.println(dataString);
72
  }  
73
  // if the file isn't open, pop up an error:
74
  else {
75
    Serial.println("error opening datalog.txt");
76
  } 
77
}
78

    
79

    
80

    
81

    
82

    
83

    
84

    
85

    
86