[136] | 1 | /*
|
---|
| 2 | SD card file dump
|
---|
| 3 |
|
---|
| 4 | This example shows how to read a file from the SD card using the
|
---|
| 5 | SD library and send it over the serial port.
|
---|
| 6 |
|
---|
| 7 | The circuit:
|
---|
| 8 | * SD card attached to SPI bus as follows:
|
---|
| 9 | ** MOSI - pin 11
|
---|
| 10 | ** MISO - pin 12
|
---|
| 11 | ** CLK - pin 13
|
---|
| 12 | ** CS - pin 4
|
---|
| 13 |
|
---|
| 14 | created 22 December 2010
|
---|
| 15 | by Limor Fried
|
---|
| 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 | // open the file. note that only one file can be open at a time,
|
---|
| 55 | // so you have to close this one before opening another.
|
---|
| 56 | File dataFile = SD.open("datalog.txt");
|
---|
| 57 |
|
---|
| 58 | // if the file is available, write to it:
|
---|
| 59 | if (dataFile) {
|
---|
| 60 | while (dataFile.available()) {
|
---|
| 61 | Serial.write(dataFile.read());
|
---|
| 62 | }
|
---|
| 63 | dataFile.close();
|
---|
| 64 | }
|
---|
| 65 | // if the file isn't open, pop up an error:
|
---|
| 66 | else {
|
---|
| 67 | Serial.println("error opening datalog.txt");
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void loop()
|
---|
| 72 | {
|
---|
| 73 | }
|
---|
| 74 |
|
---|