0

Music Maker Shield with a variable as the filename?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Music Maker Shield with a variable as the filename?

by Zlshep on Thu Aug 05, 2021 4:03 pm

Hi all! I'm hoping you can help me. I've got a Music Maker Shield with my Arduino Uno. For one reason or another I have a filename in a string. The filename is generated as such:

Code: Select all | TOGGLE FULL SIZE
String filename = "HELLO" + String(TRACKNUM) + ".wav";


The TRACKNUM is an int which is then converted to string for the filename string. This is because the TRACKNUM will be incremented every time the code runs, so that it plays a different file each time.

Can anyone give any sugggestions on how to make the musicPlayer play a file from a string variable? My code is currently:
Code: Select all | TOGGLE FULL SIZE
    musicPlayer.playFullFile(String(filename));


But this doesn't work, I've tried many iterations and variations but I'm not getting anywhere. Help!!

Zlshep
 
Posts: 4
Joined: Wed Aug 04, 2021 12:02 pm

Re: Music Maker Shield with a variable as the filename?

by Zlshep on Thu Aug 05, 2021 7:46 pm

Ok hi I have solved this!!! I want to write here for future readers in case anyone stumbles here in the years to come.

The musicPlayer.playFullFile() reads a char array, NOT a string.

In my case, all of my file names are the same length, deliberately. And there is a number that changes each time a file is played. So my code for example would be
Code: Select all | TOGGLE FULL SIZE
int tracknum = 1

string filename = "File" + string(tracknum) + ".wav"; // You have to do string(tracknum) to get a string version of the int declared above

const char filechar[10]; // you have to declare a const char array to read the details into. the [10] in square brackets is the number of characters in the array - this needs to be one more than you need, so it contains the terminator char at the end

filename.toCharArray(filechar,10); // this is where you read the string filename into the char array filechar - the 10 is the number of characters you have, as above 10 because FILE1.wav is 9 characters

musicPlayer.playFullFile(filechar); //and you're done! The file will now play.


Good job getting this far, future-starter! I hope this helps you.

Zlshep
 
Posts: 4
Joined: Wed Aug 04, 2021 12:02 pm

Re: Music Maker Shield with a variable as the filename?

by Sticks50 on Sat Sep 11, 2021 6:47 pm

I have spent half the day trying to figure this out on my own...and then in desparation...I decided to see if anyone had posted the topic. Thank you so very very much! I am doing the victory "Rocky" dance!

Sticks50
 
Posts: 5
Joined: Sun Dec 13, 2020 9:27 pm

Please be positive and constructive with your questions and comments.