Music Maker Shield with a variable as the filename?

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Zlshep
 
Posts: 16
Joined: Wed Aug 04, 2021 12:02 pm

Music Maker Shield with a variable as the filename?

Post by Zlshep »

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

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

    musicPlayer.playFullFile(String(filename));
But this doesn't work, I've tried many iterations and variations but I'm not getting anywhere. Help!!

User avatar
Zlshep
 
Posts: 16
Joined: Wed Aug 04, 2021 12:02 pm

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

Post by Zlshep »

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

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.

User avatar
Sticks50
 
Posts: 8
Joined: Sun Dec 13, 2020 9:27 pm

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

Post by Sticks50 »

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!

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”