Sub directories of SD from user input

Hello,

I have been trying to write a program which prints the contents of an SD card (able to do that from the examples given). After printing the card contents, I am want for the user to be able to type in a directory on the card within the serial monitor. Then the contents of the Sd card should be searched, if there is a match between what the user typed and a file on the card - this should then open that directory into a sub directory, the user then being able to select one of the specific files in that sub-directory.

This is what I have so far, I am able to type in the initial directory. I think the problem lies within the line

File userInput = printSound.openNextFile();

As it always opens the first directory on the card and not the one the user has typed in.

here is the rest of the code:

#include <Arduino.h>
#include <SD.h>
#include <SdFat.h>
#include <Audio.h>
#include <String.h>

//#define _SD_DAT3 46

#define numSampleFiles 12
//File root;
File currentDirectory;
File soundDirectory;

String fileExtension = ".wav";
String userInput;
char dataChar[16] = {'\0'};



char fileNameBuff[80]; //Vector<string> filenames;
char newfileNameBuff[80];
char rootSampleDir[25];
samples samps [numSampleFiles];
const long maxSampleLength = 1200000;
int numItems;

void printDir(File currentDirectory, int numTabs);
void scanFile(File printSound);

#define MOSI_PIN 11
#define SCK_PIN 13
#define CS_PIN = BUILTIN_SDCARD

void setup()
{

 Serial.begin(9600);
 //SPI.setMOSI(MOSI_PIN);
 //SPI.setSCK(SCK_PIN);

  while(!Serial);

  Serial.println("Initializing MicroSD"); // Message whilst waiting for the SD card to be inserted
  if (!SD.begin(BUILTIN_SDCARD))                  // If not inserted
  {
    Serial.println("MicroSD card not initialized"); // Infinite Loop so that it is clear no SD card has been inserted
    while (1);
  }
  Serial.println("MicroSD card initialized!"); // otherwise print message to show it has been inserted

  currentDirectory = SD.open("/");
  
  soundDirectory = SD.open("/");
  
  printDir(currentDirectory, 0);
  
  scanFile(soundDirectory);

  
  for (int i = 0; i < numSampleFiles; i++)
  {
    sprintf(samps[i].fileName, "S%d.wav", i+1);
    if (!SerialFlash.exists(samps[i].fileName)){
      SerialFlash.createErasable(samps[i].fileName, maxSampleLength);
    }
  }
  

  //See what's in our root Sample directory
  sprintf(fileNameBuff, "/");
  sprintf(rootSampleDir, "/");

  //currentDirectory = SD.open(rootSampleDir);
  //printDir(currentDirectory, 0);

  Serial.println("contents printed");
  
}


void printDir(File dir, int numTabs)
{
  //Serial.println("TESTING");
  while(true) 
  {

    File entry = dir.openNextFile();

     if (!entry) 
     {
      //currentDirectory.rewindDirectory(); //This gets stick in a constant loop?
      break;
     }
     
     else
     {
      numItems = numItems+1;
     }
     
      for(uint8_t i = 0; i < numTabs; i++)
      {
       Serial.print('\t');
      }
      Serial.print(entry.name());
      if(entry.isDirectory())
      {
        Serial.println("/");
        printDir(entry, numTabs + 1);
      }
      else
      {
        Serial.print("\t\t");
      }    
      delay(1);
    entry.close();
  }
  

  
   


void scanFile(File printSound)
{
  Serial.println("\nPlease type in a directory: ");
  while(Serial.available()==0)
  {
    //This keeps the serial looped until something is typed
  }

  
  while(1)
  {

    userInput = Serial.readString(); //Takes user input
    Serial.println(userInput);       //prints user input

    //File select = printSound.openNextFile();
    File userInput = printSound.openNextFile(); //Maybe this should be open SELECT file - not just next?
    
    

    Serial.println(userInput.name());

    if(userInput.isDirectory())
    {
      Serial.println("/");
      scanFile(userInput); 
      
      Serial.println("selected directory is: " + userInput);            
    }
      
  }

hope this makes sense. Thanks!
J

so you read in something in your String and the next line you define a local variable with the same name and of type File and just go to next file regardless of what the user typed in?

how is that supposed to work?

Okay, yes I see what your saying. I'm unsure how to check if there is a directory with the same name the user typed in and then open it if so? Would I have to declare a new function which says
if there is a directory
SD.open(...?)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.