Line | |
---|
1 | /*
|
---|
2 | Listfiles
|
---|
3 |
|
---|
4 | This example shows how print out the files in a
|
---|
5 | directory on a SD card
|
---|
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 Nov 2010
|
---|
15 | by David A. Mellis
|
---|
16 | modified 9 Apr 2012
|
---|
17 | by Tom Igoe
|
---|
18 | modified 2 Feb 2014
|
---|
19 | by Scott Fitzgerald
|
---|
20 |
|
---|
21 | This example code is in the public domain.
|
---|
22 |
|
---|
23 | */
|
---|
24 | #include <SPI.h>
|
---|
25 | #include <SD.h>
|
---|
26 |
|
---|
27 | File root;
|
---|
28 |
|
---|
29 | void setup()
|
---|
30 | {
|
---|
31 | // Open serial communications and wait for port to open:
|
---|
32 | Serial.begin(9600);
|
---|
33 | while (!Serial) {
|
---|
34 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
35 | }
|
---|
36 |
|
---|
37 | Serial.print("Initializing SD card...");
|
---|
38 | // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
|
---|
39 | // Note that even if it's not used as the CS pin, the hardware SS pin
|
---|
40 | // (10 on most Arduino boards, 53 on the Mega, 14 on the Leonardo) must be left as an output
|
---|
41 | // or the SD library functions will not work.
|
---|
42 |
|
---|
43 | if (!SD.begin(4)) {
|
---|
44 | Serial.println("initialization failed!");
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | Serial.println("initialization done.");
|
---|
48 |
|
---|
49 | root = SD.open("/");
|
---|
50 |
|
---|
51 | printDirectory(root, 0);
|
---|
52 |
|
---|
53 | Serial.println("done!");
|
---|
54 | }
|
---|
55 |
|
---|
56 | void loop()
|
---|
57 | {
|
---|
58 | // nothing happens after setup finishes.
|
---|
59 | }
|
---|
60 |
|
---|
61 | void printDirectory(File dir, int numTabs) {
|
---|
62 | while(true) {
|
---|
63 |
|
---|
64 | File entry = dir.openNextFile();
|
---|
65 | if (! entry) {
|
---|
66 | // no more files
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | for (uint8_t i=0; i<numTabs; i++) {
|
---|
70 | Serial.print('\t');
|
---|
71 | }
|
---|
72 | Serial.print(entry.name());
|
---|
73 | if (entry.isDirectory()) {
|
---|
74 | Serial.println("/");
|
---|
75 | printDirectory(entry, numTabs+1);
|
---|
76 | } else {
|
---|
77 | // files have sizes, directories do not
|
---|
78 | Serial.print("\t\t");
|
---|
79 | Serial.println(entry.size(), DEC);
|
---|
80 | }
|
---|
81 | entry.close();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.