Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Saving file names from an SD card to an array (Teensy 4.0 and Audio Shield)

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    7

    Saving file names from an SD card to an array (Teensy 4.0 and Audio Shield)

    I am trying to get the names of all the files in an SD card and save them to an array. Originally, I planned to use vector, but then I found out that stl was removed in Arduino. So I wrote some code:
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    // GUItool: begin automatically generated code
    AudioInputI2S            i2s1;           //xy=530,578
    AudioAnalyzeNoteFrequency notefreq;       //xy=995,569
    AudioConnection          patchCord1(i2s1, 0, notefreq, 0);
    AudioControlSGTL5000     sgtl5000_1;     //xy=769,647
    // GUItool: end automatically generated code
    
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    File file;
    
    int fileNum = 0;
    int everyFileNum = 0;
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
    
      delay(250);
      AudioMemory(200);
      delay(250);
    
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        // stop here if no SD card, but print a message
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
      file = SD.open("/");
      reloadFileList();
    }
    void loop() {
      // put your main code here, to run repeatedly:
    
      delay(1000);
    }
    
    void reloadFileList() {
      while (true) { //Check the number of files
        File entry = file.openNextFile();
        if (!entry) {
          Serial.print("Detected files: ");
          Serial.println(fileNum);
          break;
        }
        if (!entry.isDirectory()) {
          fileNum ++;
        }
        everyFileNum ++;
        entry.close();
      }
    
      String fileList[fileNum];
    
      file.close();
      file = SD.open("/");
      int i = 0;
      while (i < fileNum) {
        File entry = file.openNextFile();
        Serial.println(i);
        if (!entry.isDirectory()) {
          fileList[i] = entry.name();
          i ++;
        }
        entry.close();
        Serial.println(fileList[i]);
      }
    
    }
    So this basically runs through all the files and saves the number of files. And then it makes an array using the saved value. And then it runs through all the files again and this time it saves them to the array. It's supposed to output the number of files and the file names but the actual output is this:
    Click image for larger version. 

Name:	화면 캡처 2021-08-07 094335.png 
Views:	7 
Size:	5.7 KB 
ID:	25523

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,750
    entry.name() returns a pointer to a C-style string - not to a String. Casting it will fix the problem:
    Code:
    fileList[i] = (String)entry.name();
    Pete

  3. #3
    Senior Member
    Join Date
    Apr 2020
    Location
    DFW area in Texas
    Posts
    297
    Looking just at your "while" loop, you seem to be incrementing "i" too soon.

    Quote Originally Posted by Taemin View Post

    Code:
      while (i < fileNum) {
        File entry = file.openNextFile();
        Serial.println(i);
        if (!entry.isDirectory()) {
          fileList[i] = entry.name();
          i ++;      <== don't increment here - remove this line
        }
        entry.close();
        Serial.println(fileList[i]);  <== if "i" is incremented earlier, then you are trying to print "the next" filename, instead of the filename just stored - maybe increment with "filelist[i++]" here instead
      }
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •