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

Status
Not open for further replies.

Taemin

Member
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:
화면 캡처 2021-08-07 094335.png
 
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
 
Looking just at your "while" loop, you seem to be incrementing "i" too soon.

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
  }

}
 
Status
Not open for further replies.
Back
Top