root / arduino-1.0 / libraries / SD / examples / DumpFile / DumpFile.ino @ 58d82c77
History | View | Annotate | Download (1.48 KB)
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 |
|
16 |
This example code is in the public domain. |
17 |
|
18 |
*/ |
19 |
|
20 |
#include <SD.h> |
21 |
|
22 |
// On the Ethernet Shield, CS is pin 4. Note that even if it's not |
23 |
// used as the CS pin, the hardware CS pin (10 on most Arduino boards, |
24 |
// 53 on the Mega) must be left as an output or the SD library |
25 |
// functions will not work. |
26 |
const int chipSelect = 4; |
27 |
|
28 |
void setup() |
29 |
{ |
30 |
Serial.begin(9600); |
31 |
Serial.print("Initializing SD card..."); |
32 |
// make sure that the default chip select pin is set to |
33 |
// output, even if you don't use it: |
34 |
pinMode(10, OUTPUT); |
35 |
|
36 |
// see if the card is present and can be initialized: |
37 |
if (!SD.begin(chipSelect)) { |
38 |
Serial.println("Card failed, or not present"); |
39 |
// don't do anything more: |
40 |
return; |
41 |
} |
42 |
Serial.println("card initialized."); |
43 |
|
44 |
// open the file. note that only one file can be open at a time, |
45 |
// so you have to close this one before opening another. |
46 |
File dataFile = SD.open("datalog.txt"); |
47 |
|
48 |
// if the file is available, write to it: |
49 |
if (dataFile) { |
50 |
while (dataFile.available()) { |
51 |
Serial.write(dataFile.read()); |
52 |
} |
53 |
dataFile.close(); |
54 |
} |
55 |
// if the file isn't open, pop up an error: |
56 |
else { |
57 |
Serial.println("error opening datalog.txt"); |
58 |
} |
59 |
} |
60 |
|
61 |
void loop() |
62 |
{ |
63 |
} |
64 |
|