| /* | /* | ||||
| SD card basic file example | |||||
| SD card basic directory list example | |||||
| This example shows how to create and destroy an SD card file | This example shows how to create and destroy an SD card file | ||||
| The circuit: | The circuit: | ||||
| */ | */ | ||||
| #include <SD.h> | #include <SD.h> | ||||
| #include <SPI.h> | |||||
| File root; | |||||
| // change this to match your SD shield or module; | // change this to match your SD shield or module; | ||||
| // Arduino Ethernet shield: pin 4 | |||||
| // Adafruit SD shields and modules: pin 10 | |||||
| // Sparkfun SD shield: pin 8 | |||||
| // Teensy audio board: pin 10 | |||||
| // Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD | |||||
| // Wiz820+SD board: pin 4 | |||||
| // Teensy 2.0: pin 0 | // Teensy 2.0: pin 0 | ||||
| // Teensy++ 2.0: pin 20 | // Teensy++ 2.0: pin 20 | ||||
| const int chipSelect = 4; | |||||
| // Wiz820+SD board: pin 4 | |||||
| // Teensy audio board: pin 10 | |||||
| // Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD | |||||
| const int chipSelect = 10; | |||||
| void setup() | void setup() | ||||
| { | { | ||||
| //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD: | |||||
| //Uncomment these lines for Teensy 3.x Audio Shield (Rev C) | |||||
| //SPI.setMOSI(7); // Audio shield has MOSI on pin 7 | //SPI.setMOSI(7); // Audio shield has MOSI on pin 7 | ||||
| //SPI.setSCK(14); // Audio shield has SCK on pin 14 | //SPI.setSCK(14); // Audio shield has SCK on pin 14 | ||||
| ; // wait for serial port to connect. | ; // wait for serial port to connect. | ||||
| } | } | ||||
| Serial.print("Initializing SD card..."); | Serial.print("Initializing SD card..."); | ||||
| if (!SD.begin(chipSelect)) { | if (!SD.begin(chipSelect)) { | ||||
| } | } | ||||
| Serial.println("initialization done."); | Serial.println("initialization done."); | ||||
| root = SD.open("/"); | |||||
| File root = SD.open("/"); | |||||
| printDirectory(root, 0); | printDirectory(root, 0); | ||||
| // nothing happens after setup finishes. | // nothing happens after setup finishes. | ||||
| } | } | ||||
| void printDirectory(File dir, int numTabs) { | |||||
| void printDirectory(File dir, int numSpaces) { | |||||
| while(true) { | while(true) { | ||||
| File entry = dir.openNextFile(); | |||||
| File entry = dir.openNextFile(); | |||||
| if (! entry) { | if (! entry) { | ||||
| // no more files | |||||
| //Serial.println("**nomorefiles**"); | |||||
| //Serial.println("** no more files **"); | |||||
| break; | break; | ||||
| } | } | ||||
| for (uint8_t i=0; i<numTabs; i++) { | |||||
| Serial.print('\t'); | |||||
| } | |||||
| printSpaces(numSpaces); | |||||
| Serial.print(entry.name()); | Serial.print(entry.name()); | ||||
| if (entry.isDirectory()) { | if (entry.isDirectory()) { | ||||
| Serial.println("/"); | Serial.println("/"); | ||||
| printDirectory(entry, numTabs+1); | |||||
| printDirectory(entry, numSpaces+2); | |||||
| } else { | } else { | ||||
| // files have sizes, directories do not | // files have sizes, directories do not | ||||
| Serial.print("\t\t"); | |||||
| printSpaces(48 - numSpaces - strlen(entry.name())); | |||||
| Serial.print(" "); | |||||
| Serial.println(entry.size(), DEC); | Serial.println(entry.size(), DEC); | ||||
| } | } | ||||
| entry.close(); | entry.close(); | ||||
| } | } | ||||
| } | } | ||||
| void printSpaces(int num) { | |||||
| for (int i=0; i < num; i++) { | |||||
| Serial.print(" "); | |||||
| } | |||||
| } | |||||