Pass name (variable) to sd.open

excuse me for post again, Im sure im overlooking how to do this but none of the examples i have seen give me a way to do this....I am completely lost on how to do this. I would specify each file in the function that runs it, but can't have duplicating code blocks :frowning:

sd open command id like to replace...

File file = SD.open("/sound.wav");

I have reused my enum but not sure how to utilize it for this function.

Example cases

   case updatecomplete:
 File file = SD.open("/wav/updatecomplete.wav");
            printf("/wav/updatecomplete.wav\n");
            break;
    
    
    case updatevoice:
       File file = SD.open("/wav/updatevoice.wav ");
            printf("/wav/updatevoice.wav \n");
            break;

Thank your for your time.

   case updatecomplete:
 File file = SD.open("/wav/updatecomplete.wav");
            printf("/wav/updatecomplete.wav\n");
            break;

You don't say what the problem is but it sounds like one of variable scope. The code snippet above will declare a variable of type File named file with the value given, but its scope will be limited to the code block for the updatecomplete case

What exactly happens when you use the code as posted ?

I note that despite the title of the topic, the code snippet that you posted does not try to pass a variable to the SD.open() function

My apologies, what I was trying to ask was how to pass a variable instead of just the direct path...

What have you tried ?
What variable type did you try ?
What code did you use ?
What error messages did you get ?
Which Arduino board are you using ?

What have you tried ?
i've tried passing the effect variable from the switch, although not sure where to put it...I mean within the function. I just need to figure out how to replace file file = sd.open(filepath);
What variable type did you try ?
Global im assuming...
What code did you use ?
This is the code for the switch case


void playWAV (char effect)
{
  Serial.println("Entered playWAV");
  switch (effect)
  {
    case high_alarm:
              File file = SD.open("/wav/high_alarm.wav");
            printf("/wav/high_alarm.wav\n");
            break;
   
    case high_low_warning:
              File file = SD.open("/wav/high_low_warning.wav");
            printf("/wav/high_low_warning.wav\n");
            break;
    
   
    case highalarmstillvoice:
              File file = SD.open("/wav/highalarmstillvoice.wav");
            printf("/wav/highalarmstillvoice.wav\n");
            break;
    
   
    case highalarmvoice:
              File file = SD.open("/wav/highalarmvoice.wav");
            printf("/wav/highalarmvoice.wav\n");
            break;
    
  
    case highwarningstillvoice:
              File file = SD.open("/wav/highwarningstillvoice.wav");
            printf("/wav/highwarningstillvoice.wav\n");
            break;
    
   
    case highwarningvoice:
              File file = SD.open("/wav/highwarningvoice.wav");
            printf("/wav/highwarningvoice.wav\n");
            break;
    
   
    case inrangestillvoice:
              File file = SD.open("/wav/inrangestillvoice.wav");
            printf("/wav/inrangestillvoice.wav\n");
            break;
    
    
    case low_alarm:
              File file = SD.open("/wav/low_alarm.wav");
            printf("/wav/low_alarm.wav\n");
            break;
    
    
    case lowalarmstillvoice:
              File file = SD.open("/wav/lowalarmstillvoice.wav");
            printf("/wav/lowalarmstillvoice.wav\n");
            break;
    
    
    case lowalarmvoice:
              File file = SD.open("/wav/lowalarmvoice.wav");
            printf("/wav/lowalarmvoice.wav\n");
            break;
    
   
    case lowwarningstillvoice:
              File file = SD.open("/wav/lowwarningstillvoice.wav");
            printf("/wav/lowwarningstillvoice.wav\n");
            break;
    
 
    case lowwarningvoice:
              File file = SD.open("/wav/lowwarningvoice.wav");
            printf("/wav/lowwarningvoice.wav\n");
            break;
    
  
    case nightmodeoff:
              File file = SD.open("/wav/nightmodeoff.wav");
            printf("/wav/nightmodeoff.wav\n");
            break;
    
  
    case nightmodeon:
              File file = SD.open("/wav/nightmodeon.wav");
            printf("/wav/nightmodeon.wav\n");
            break;
    
   
    case nightmodereminder:
              File file = SD.open("/wav/nightmodereminder.wav");
            printf("/wav/nightmodereminder.wav\n");
            break;
    
    
    case no_readings_alarm:
              File file = SD.open("/wav/no_readings_alarm.wav");
            printf("/wav/no_readings_alarm.wav\n");
            break;
    
  
    case noreadingsvoice:
              File file = SD.open("/wav/noreadingsvoice.wav");
            printf("/wav/noreadingsvoice.wav\n");
            break;
    
   
    case normalrangevoice:
              File file = SD.open("/wav/normalrangevoice.wav");
            printf("/wav/normalrangevoice.wav\n");
            break;
    
   
    case persistenthigh:
              File file = SD.open("/wav/persistenthigh.wav");
            printf("/wav/persistenthigh.wav\n");
            break;
    
    
    case startup:
              File file = SD.open("/wav/startup.wav");
            printf("/wav/startup.wav\n");
            break;
    
    
    case updatecomplete:
 File file = SD.open("/wav/updatecomplete.wav");
            printf("/wav/updatecomplete.wav\n");
            break;
    
    
    case updatevoice:
       File file = SD.open("/wav/updatevoice.wav ");
            printf("/wav/updatevoice.wav \n");
            break;
  }

Code for the function.


File file = SD.open("/sound.wav");  // 44100Hz, 16bit, linear PCM
  file.seek(22);
  int ch = file.read();
  file.seek(offset);
  I2S_Init();
  while (file.readBytes(data, sizeof(data))) {
    if (ch == 2) I2S_Write(data, sizeof(data));
    else if (ch == 1) {
      for (int i = 0; i < sizeof(data); ++i) 
      {
        stereoData[4 * (i / 2) + i % 2] = data[i];
        stereoData[4 * (i / 2) + i % 2 + 2] = data[i];
      }
      I2S_Write(stereoData, sizeof(stereoData));
    }
  }
  file.close();
  for (int i = 0; i < sizeof(data); ++i) data[i] = 0; // to prevent buzzing
  for (int i = 0; i < 5; ++i) I2S_Write(data, sizeof(data));
}

What error messages did you get ?
I am currently only getting one message I cant seem to get past...

transfer of control bypasses initialization of: -- variable "file" (declared at line 255) -- variable "file" (declared at line 260) -- variable "file" (declared at line 266) -- variable "file" (declared at line 272) -- variable "file" (declared at line 278) -- variable "file" (declared at line 284) -- variable "file" (declared at line 290) -- variable "file" (declared at line 296) -- variable "file" (declared at line 302) -- variable "file" (declared at line 308) -- variable "file" (declared at line 314) -- variable "file" (declared at line 320) -- variable "file" (declared at line 326) -- variable "file" (declared at line 332) -- variable "file" (declared at line 338) -- variable "file" (declared at line 344) -- variable "file" (declared at line 350) -- variable "file" (declared at line 356) -- variable "file" (declared at line 362) -- variable "file" (declared at line 368) -- variable "file" (declared at line 374)

I read i needed to put { } in the case funtions but everytime i do everything starts throwing an error.....

perhaps?

void fSendToFile( void * parameter )
{
  if (!SD.begin())
  {
    log_i("Card Mount Failed");
  }
  String toFile = "";
  toFile.reserve( toFileSize );
  for (;;)
  {
    if ( xQueueReceive(Q_toFIle, &toFile, portMAX_DELAY) == pdTRUE )
    {
      //log_i( "%s\n", toFile.c_str() );
      appendFile(SD, "/data.txt", toFile.c_str() + '\n');
      toFile = "";
    }
    //log_i( " high watermark %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}

Im sorry, can you explain this to me? How would that accept multiple cases? Or is that a different function im not understanding....

The effect variable is a char and it can hold only a single character

When you use a literal string it is a pointer to a string so it looks like the open() function needs to be passed a pointer to a string. Try

char * effect = "/wav/high_alarm.wav"";
playWAV(effect);

void playWAV (char * effect)
{
  Serial.println("Entered playWAV");
  switch (effect)
  {
    case high_alarm:
              File file = SD.open(effect);
            printf("/wav/high_alarm.wav\n");
            break;
   }

Below is the image of the errors i get just placing the code above, I do have enum I can use with this switch case if that makes it easier. I have tried the enum and it still throws the error about initialization like in the earlier post by me. Maybe im trying to do your code wrong? I replaced the switch case with your code. I am sorry if im coming of stupid, im just really new to all of this.

image

How would we know unless you post your code, all of it

Here is the entire code. alot of it was a old project that the author has given me permission to modify and add new features of which might be published to the master branch if i can figure out how to do this audio case...
m5ns.ino (109.7 KB)

I see that you literally copied what I posted into your code without understanding it so now you have code that is not in a function which will cause an error

What I was suggesting was that when calling the playWAV() function the parameter should either be a constant string such as "/wav/high_alarm.wav\n" or a char * variable with a value of "/wav/high_alarm.wav" and that the parameter type of playWAV() should be char * to match what was being sent

Rather than dealing with 33,000 lines of code I suggest that you create a short but complete example of opening a file in the way that I have suggested

In addition to the problems above the code that you posted calls the playWAV() function from within the playWAV() function which is never going to work

My apologies, I believe I understand now, I will create a small sketch with the info provided. Im still learning and I appreciate you being patient with me, I have a disability that makes it where I can get confused easily, and your patience with me is a very appreciated factor.

Ok, so after testing your snippet of code, and it worked...I decided to try and implement it to my code....I am only running into 1 problem and 2 questions.

Is a value of const char* only a single letter or can it be a number or would that be an int value?

If it is single letter like im suspecting...that means im limited to 26 cases and no more correct?

Code with const char* and switch case


void playWav (const char* test, int volume)
{
File file = (test);  // 44100Hz, 16bit, linear PCM
  file.seek(22);
  int ch = file.read();
  file.seek(offset);
  I2S_Init();
  while (file.readBytes(data, sizeof(data))) {
    if (ch == 2) I2S_Write(data, sizeof(data));
    else if (ch == 1) {
      for (int i = 0; i < sizeof(data); ++i) 
      {
        stereoData[4 * (i / 2) + i % 2] = data[i];
        stereoData[4 * (i / 2) + i % 2 + 2] = data[i];
      }
      I2S_Write(stereoData, sizeof(stereoData));
    }
  }
  file.close();
  for (int i = 0; i < sizeof(data); ++i) data[i] = 0; // to prevent buzzing
  for (int i = 0; i < 5; ++i) I2S_Write(data, sizeof(data));

  
    //Serial.println("Entered paciplay_day");
    //printf("Entered paciplay_day\n");

    switch (test)
    {case '1':
              file = SD.open("/wav/high_alarm.wav");
            printf("/wav/high_alarm.wav\n");
            break;
   
    case '2':
              file = SD.open("/wav/high_low_warning.wav");
            printf("/wav/high_low_warning.wav\n");
            break;
    
   
    case '3':
              file = SD.open("/wav/highalarmstillvoice.wav");
            printf("/wav/highalarmstillvoice.wav\n");
            break;
    
   
    case '4':
              file = SD.open("/wav/highalarmvoice.wav");
            printf("/wav/highalarmvoice.wav\n");
            break;
    
  
    case '5':
              file = SD.open("/wav/highwarningstillvoice.wav");
            printf("/wav/highwarningstillvoice.wav\n");
            break;
    
   
    case '6':
              file = SD.open("/wav/highwarningvoice.wav");
            printf("/wav/highwarningvoice.wav\n");
            break;
    
   
    case '7':
              file = SD.open("/wav/inrangestillvoice.wav");
            printf("/wav/inrangestillvoice.wav\n");
            break;
    
    
    case '8':
              file = SD.open("/wav/low_alarm.wav");
            printf("/wav/low_alarm.wav\n");
            break;
    
    
    case '9':
              file = SD.open("/wav/lowalarmstillvoice.wav");
            printf("/wav/lowalarmstillvoice.wav\n");
            break;
    
    
    case '10':
              file = SD.open("/wav/lowalarmvoice.wav");
            printf("/wav/lowalarmvoice.wav\n");
            break;
    
   
    case '11':
              file = SD.open("/wav/lowwarningstillvoice.wav");
            printf("/wav/lowwarningstillvoice.wav\n");
            break;
    
 
    case '12':
              file = SD.open("/wav/lowwarningvoice.wav");
            printf("/wav/lowwarningvoice.wav\n");
            break;
    
  
    case '13':
              file = SD.open("/wav/nightmodeoff.wav");
            printf("/wav/nightmodeoff.wav\n");
            break;
    
  
    case '14':
              file = SD.open("/wav/nightmodeon.wav");
            printf("/wav/nightmodeon.wav\n");
            break;
    
   
    case '15':
              file = SD.open("/wav/nightmodereminder.wav");
            printf("/wav/nightmodereminder.wav\n");
            break;
    
    
    case '16':
              file = SD.open("/wav/no_readings_alarm.wav");
            printf("/wav/no_readings_alarm.wav\n");
            break;
    
  
    case '17':
              file = SD.open("/wav/noreadingsvoice.wav");
            printf("/wav/noreadingsvoice.wav\n");
            break;
    
   
    case '18':
              file = SD.open("/wav/normalrangevoice.wav");
            printf("/wav/normalrangevoice.wav\n");
            break;
    
   
    case '19':
              file = SD.open("/wav/persistenthigh.wav");
            printf("/wav/persistenthigh.wav\n");
            break;
    
    
    case '20':
              file = SD.open("/wav/startup.wav");
            printf("/wav/startup.wav\n");
            break;
    
    
    case '21':
 file = SD.open("/wav/updatecomplete.wav");
            printf("/wav/updatecomplete.wav\n");
            break;
    
    
    case '22':
       file = SD.open("/wav/updatevoice.wav ");
            printf("/wav/updatevoice.wav \n");
            break;

I currently have them as numbers before i realized it was asking for const char*....Thank you for your help with that code trying it helped me realize where i went wrong :slight_smile:

You need to read up on pointers, which is what a char * variable is. The actual value of the char * is an int but its purpose is not to hold an int, rather that int is used by the compiler as a pointer to where the value of the variable is stored in memory

So, when you declare a variable like this

char * effect = "/wav/high_alarm.wav"";

you are saying "there is a string of characters in memory somewhere and you can read it back by using the variable named effect. Does this answer your question about what a char * variable can hold ?

The same goes for variables of any type, not just chars, so

int * wibble = 123;

is a pointer to where an int is stored and so on
As I said, read up on pointers

Yes that does, but leads me to another quick question....if you defined

char * effect = "/wav/high_alarm.wav"

you would not be able to have multiple effect pointers (hope thats what they represent) so you'd need a different name for every variable right? Forgive me if im way off here. i mean every file not every variable.

You would need multiple pointer variables or better an array of values used like this

char * anArray[] = {"zero", "one", "two"};

void setup()
{
  Serial.begin(115200);
  for (int c = 0; c < 3; c++)
  {
    aFunction(anArray[c]);
  }
}

void loop()
{
}

void aFunction(char * message)
{
  Serial.println(message);
}

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