// SD_test.ino file 11/2/2024 #include // include the SD libary // set variables needed for SD card File root; String FileNames[250]; String NewFileNames[250]; int m=0; int mm=0; int fileCountOnSD=0; const int chipSelect = 53; //set this to what pin you are using const int beepers = 46; // beeper pin void setup() { Serial.begin(115200); // start serial for serial monitor pinMode(beepers, OUTPUT); //make beeper pin outpu digitalWrite (beepers, LOW); // set beeper to low so it doesn't keep beeping on startup } void loop() { beeper(1000); // indicate that we are starting with a beep ReadSD(); // subroutine that initializes the SD card and reads its content into FileNames[] and prints file and size PrintNamer(); // print out raw names of all files in FileNames[] FileCleaner(); // print out only files that end with .TXT and put in array NewFileNames[] - only want track files Serial.print("finished setup, number of track files= "); Serial.println(fileCountOnSD); beeper(3000); // indicate that we are done with a long beep while(1); // stop the program from repeating and just sit there } //****************************************************************************************************** void ReadSD() { while (!Serial); Serial.print("Initializing SD card..."); // initialize SD card and print out if there is a problem if (!SD.begin(chipSelect)) { Serial.println("initialization failed. Things to check:"); Serial.println("1. is a card inserted?"); Serial.println("2. is your wiring correct?"); Serial.println("3. did you change the chipSelect pin to match your shield or module?"); Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!"); while (true); // oops, there was a problem so stop here and wait } root = SD.open("/"); // open card and read contents printDirectory(root, 0); // print out contents using printDirectory routine root.close(); // close the card Serial.println("initialization done."); // let you know it is done reading contents } //****************************************************************************************************** void PrintNamer() // print out names of files that are stored in FileNames[] { for (int m=0; m<=fileCountOnSD; m++) // fileCountOnSD contains the number of files read on the SD card established in printDirectory routine { Serial.print(m); // print out number of the file Serial.print("-----"); // just a bit of space Serial.println(FileNames[m]); // print out the filename in array } } //********************************************************************************************************** void FileCleaner() // Select files on SD card that are .txt files only and put in array NewFileNames[] { mm=0; // set counter to zero for (int m=0; m<=fileCountOnSD; m++) // go through files one at a time and select .txt files { if(FileNames[m].endsWith("TXT")) // select .txt files only { NewFileNames[mm]=FileNames[m]; //put file into NewFileNames[] if it is a .txt file mm++; // increment counter } } Serial.println(" "); // print out a space on serial monitor fileCountOnSD=mm-1; // correct fieCountOnSD to reflect number of files that are in NewFileNames[] for (int m=0; m<=fileCountOnSD; m++) // print out files in NewFileNames[] { Serial.print(m); // print number of file Serial.print(" "); // print space Serial.println(NewFileNames[m]); // print out each file name } } //************************************************************************************************************** void beeper(int duration) { digitalWrite (beepers, HIGH); // set beeper to on delay(duration); // wait a time in ms for beeper to beep digitalWrite (beepers, LOW); // turn off beeper } //*************************************************************************************************************** void printDirectory(File dir, int numTabs) { m=0; // zero out m fileCountOnSD=0; // sero out fileCountOnSD while (true) // keep going until you get to break { File entry = dir.openNextFile(); // get next file Serial.print(" "); // print a space on serial monitor FileNames[m]=entry.name(); // put file name into array FileNames[] fileCountOnSD++; // increment fileCountOnSD m=m+1; // increment m if (! entry) { // if your reached the end, break out break; // break out of while loop } entry.close(); // close the SD card } } //*****************************************************************************************************************