Passing a character string to a routine

I'm trying to build an application that will use the TMRpcm library to play a variety of wav files.

The idea is that I will have an array of file names and, depending on a button selection, one of those file names will be passed to a routine that will play said file using TMRpcm.play.

The TMRpcm.play function expects a file name as follows:
TMRpcm.play("filename.wav")

I tried to set a String variable (playtone) that would include the filename and brackets and pass that to my routine, but that's not working.

I don't know if I'm trying to do something that's impossible, or simply experiencing some sort of brain cramp preventing me from seeing the obvious. Regardless, any help is appreciated.

Thanks.

Code follows:

#include <SD.h>                      // SD library
#define SD_ChipSelectPin 10  //pin connected to CS pin on SD card.
/* Other SD card pin connections:
 *  CS  - 10
 *  SCK - 13
 *  MOSI  - 11
 *  MOSO  - 12
 */
#include <TMRpcm.h>          //  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch
int buttonInput = 0; // current analog value read from pin A0
int tuneSelection = 0; // which set of songs/tunes selected based on reading of pin A1
String playtone;

void setup(){
/Set the volume and check that the SD card is installed and operational.

  tmrpcm.speakerPin = 9; // speaker output pin
  
  Serial.begin(9600);
  while (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    // If card not ready, flash red pin
    Serial.println("SD fail"); 
    delay (1000);                // wait for a second
    }
}

void loop(){   
  buttonInput = analogRead(A0);  //Check if a button has been pressed
  if (buttonInput >= 0 && buttonInput <= 200) {
        String playtone = "Raffi1.wav";
        playTune(playtone);
  }
 }
 

void playTune (String playtone){
  tmrpcm.play(playtone);
  while (tmrpcm.isPlaying()){
  delay(500);
   }
}

Hello

Try tmrpcm.play( playtone.c_str() );

I recommend to replace the String with a C-string

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

That is not what the sketch that you posted does. In any case, why are you using a String in the first place when a C style string would have done the job ?

What type of variable does the tmrpcm.play() expect to be passed ?

From the header file

    void play(char* filename, unsigned long seekPoint=0);

it is a C style string rather than a String

Thanks for the reminder. Post changed as suggested.

To confuse things further you have more than one String object named playtone in your sketch. One is a global variable and the other is limited to the scope of the code block of an if statement

Thanks. I’m struggling with trying to do a project while learning C. The last programming I did was in COBOL and FORTRAN, so I have a pretty steep learning curve ahead. Questions like mine are likely frustrating for the ‘old hands’ but your patience in answering is very helpful.