Getting the number of files in an SD card - Teensy 4.0 and Audio Shield

Status
Not open for further replies.

Taemin

Member
I am trying to get the number of files in an SD card using the SD.h library. I am using Teensy 4.0 and the Audio Shield. There are 4 .wav files in my 16GB SD card but for some reason, the output(which shows the number each time it goes through one file and shows the final number at the end) stops at 2 and shows "Detected files: " without the D, e, and the number missing.

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;


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) {
    delay(1000);
    File entry =  file.openNextFile();
    if(entry.isDirectory()) {
      fileNum ++;
      Serial.println(fileNum);
    }
    if (!entry) {
      Serial.println("Detected files: " + fileNum);
      break;
    }
    entry.close();
  }
}

Anyone know why?
 
Does
Code:
void reloadFileList() {
  
  while(true) {
    delay(1000);
    File entry =  file.openNextFile();
    if (!entry) {
      Serial.println("Detected files: " + fileNum);
      break;
    }
    if(!entry.isDirectory()) {
      fileNum ++;
      Serial.println(fileNum);
    }
    entry.close();
  }
}
behave differently?
note: if entry is directory I did not count it as file
 
It did behave differently. Now it does count up to 4, but the final output is still weird. On my original question, I forgot to attach a photo but this is what I meant
화면 캡처 2021-08-01 191433.png
 
Thanks now it works. I wonder why it did that. Maybe it had a problem adding string and int together
 
Status
Not open for further replies.
Back
Top