[136] | 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 | modified 9 Apr 2012
|
---|
| 17 | by Tom Igoe
|
---|
| 18 |
|
---|
| 19 | This example code is in the public domain.
|
---|
| 20 |
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | #include <SPI.h>
|
---|
| 24 | #include <SD.h>
|
---|
| 25 |
|
---|
| 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,
|
---|
| 28 | // 53 on the Mega) must be left as an output or the SD library
|
---|
| 29 | // functions will not work.
|
---|
| 30 | const int chipSelect = 4;
|
---|
| 31 |
|
---|
| 32 | void setup()
|
---|
| 33 | {
|
---|
| 34 | // Open serial communications and wait for port to open:
|
---|
| 35 | Serial.begin(9600);
|
---|
| 36 | while (!Serial) {
|
---|
| 37 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | Serial.print("Initializing SD card...");
|
---|
| 42 | // make sure that the default chip select pin is set to
|
---|
| 43 | // output, even if you don't use it:
|
---|
| 44 | // pinMode(10, OUTPUT);
|
---|
| 45 |
|
---|
| 46 | // see if the card is present and can be initialized:
|
---|
| 47 | if (!SD.begin(chipSelect)) {
|
---|
| 48 | Serial.println("Card failed, or not present");
|
---|
| 49 | // don't do anything more:
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 | Serial.println("card initialized.");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void loop()
|
---|
| 56 | {
|
---|
| 57 | // make a string for assembling the data to log:
|
---|
| 58 | String dataString = "";
|
---|
| 59 |
|
---|
| 60 | // read three sensors and append to the string:
|
---|
| 61 | for (int analogPin = 0; analogPin < 3; analogPin++) {
|
---|
| 62 | int sensor = analogRead(analogPin);
|
---|
| 63 | dataString += String(sensor);
|
---|
| 64 | if (analogPin < 2) {
|
---|
| 65 | dataString += ",";
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // open the file. note that only one file can be open at a time,
|
---|
| 70 | // so you have to close this one before opening another.
|
---|
| 71 | File dataFile = SD.open("datalog.txt", FILE_WRITE);
|
---|
| 72 |
|
---|
| 73 | // if the file is available, write to it:
|
---|
| 74 | if (dataFile) {
|
---|
| 75 | dataFile.println(dataString);
|
---|
| 76 | dataFile.close();
|
---|
| 77 | // print to the serial port too:
|
---|
| 78 | Serial.println(dataString);
|
---|
| 79 | }
|
---|
| 80 | // if the file isn't open, pop up an error:
|
---|
| 81 | else {
|
---|
| 82 | Serial.println("error opening datalog.txt");
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 |
|
---|