Hi, I am trying to make a arduino mp3palyer using a Arduino UNO and a Vcodec chip from adafruit
(project page here- https://learn.adafruit.com/adafruit-vs1053-mp3-aac-ogg-midi-wav-play-and-record-
codec-tutorial/simple-audio-player-wiring). So I have the library, everything is wired right, and I got a
sample code working. So my next step was to make it more like a regular mp3 player by adding the
RANDOM song function. This function is when it shuffles the song list. Fast forward a day and I make a
program that can create these shuffled lists, I add it to the music sample code and it involves variables
such as "randomInt1", there are 23 of them. So now when I play a sound file and I have to write the
name so I write ("music"(randomInt1)".mp3"). Now, the music files are named "music1.mp3". The idea
is that I play the sound file ("music"(randomInt1)".mp3") and because the integer is random it will
randomly choose a sound file. But the error says that and expression cannot be used as a function, is
there a solution or do I need to scrap the whole thing?
Here is the error section of my code:
while(songSequence != 23){
if(songSequence = 0){
musicPlayer.playFullFile("music"(randomInt1)".mp3");
songSequence++;
}
if(songSequence = 1){
musicPlayer.playFullFile("music"(randomInt2)".mp3");
songSequence++;
}
if(songSequence = 2){
musicPlayer.playFullFile("music"(randomInt3)".mp3");
songSequence++;
}
if(songSequence = 3){
musicPlayer.playFullFile("music"(randomInt4)".mp3");
songSequence++;
}
if(songSequence = 4){
musicPlayer.playFullFile("music"(randomInt5)".mp3");
songSequence++;
}
if(songSequence = 5){
musicPlayer.playFullFile("music"(randomInt6)".mp3");
songSequence++;
}
if(songSequence = 6){
musicPlayer.playFullFile("music"(randomInt7)".mp3");
songSequence++;
}
if(songSequence = 7){
musicPlayer.playFullFile("music"(randomInt8)".mp3");
songSequence++;
}
if(songSequence = 8){
musicPlayer.playFullFile("music"(randomInt9)".mp3");
songSequence++;
}
if(songSequence = 9){
musicPlayer.playFullFile("music"(randomInt10)".mp3");
songSequence++;
}
if(songSequence = 10){
musicPlayer.playFullFile("music"(randomInt11)".mp3");
songSequence++;
}
if(songSequence = 11){
musicPlayer.playFullFile("music"(randomInt12)".mp3");
songSequence++;
}
if(songSequence = 12){
musicPlayer.playFullFile("music"(randomInt13)".mp3");
songSequence++;
}
if(songSequence = 13){
musicPlayer.playFullFile("music"(randomInt14)".mp3");
songSequence++;
}
if(songSequence = 14){
musicPlayer.playFullFile("music"(randomInt15)".mp3");
songSequence++;
}
if(songSequence = 15){
musicPlayer.playFullFile("music"(randomInt16)".mp3");
songSequence++;
}
if(songSequence = 16){
musicPlayer.playFullFile("music"(randomInt17)".mp3");
songSequence++;
}
if(songSequence = 17){
musicPlayer.playFullFile("music"(randomInt18)".mp3");
songSequence++;
}
if(songSequence = 18){
musicPlayer.playFullFile("music"(randomInt19)".mp3");
songSequence++;
}
if(songSequence = 19){
musicPlayer.playFullFile("music"(randomInt20)".mp3");
songSequence++;
}
if(songSequence = 20){
musicPlayer.playFullFile("music"(randomInt21)".mp3");
songSequence++;
}
if(songSequence = 21){
musicPlayer.playFullFile("music"(randomInt22)".mp3");
songSequence++;
}
if(songSequence = 22){
musicPlayer.playFullFile("music"(randomInt23)".mp3");
songSequence++;
}
}
delay(100);
}
Also I think that I might be in the wrong category. Not sure if this topic belongs somewhere else.
- Why double-space your text. It makes it much harder to read.
- Why didn't you post your code between code tags? They're generated using the </> button in the post and "Reply" windows. Also, you should post all of your code, not just a snippet. Among other things, we can't see which library you're using to check on it's correct usage.
Could you please edit your post and fix this?
If you really want help, make it as easy as possible for potential helpers.
You can't do this:-
musicPlayer.playFullFile("music"(randomInt23)".mp3");
You could build the full name as a string 'on-the-fly', then pass the string to 'playFullFile()'.
Something like this:-
randomSeed(analogRead(A0)); // Read an unused analogue input for this.
int songNum = random(1, 11); // Generate a random song number.
char fullSongName[20]; // Make this array big enough to hold the longest song name plus one extra char for the terminating zero.
sprintf_P(fullSongName, PSTR("music%d.mp3"), songNum); // Build the name.
musicPlayer.playFullFile(fullSongName); // Play the song.
You'll need to set your own values in the call to 'random()'. 'random(1, 11);' will generate a random number between 1 and 10.
I just saw your last post - "Programming Questions" would have been more appropriate.
You can test it in the serial monitor, minus the call to 'musicPlayer.playFullFile()' using this code:-
void setup()
{
Serial.begin(9600);
}
void loop()
{
randomSeed(analogRead(A0)); // Read an unused analogue input for this.
int songNum = random(1, 11); // Generate a random song number.
char fullSongName[20]; // Make this array big enough to hold the longest song name plus one extra char for the terminating zero.
sprintf_P(fullSongName, PSTR("music%d.mp3"), songNum); // Build the name.
// musicPlayer.playFullFile(fullSongName); // Play the song.
Serial.println(fullSongName);
delay(1000);
}
Just set the baud rate in your serial monitor to 9600.
I apologize, I am new as you can tell. I thought it would be easier to read if there was space between the lines, thanks for letting me know. As for the code It was way too much for the comment box to handle, I hard code a lot its easier for me. I tried the code tag and it said that It exceeded the character limit.
zackous:
I apologize, I am new as you can tell. I thought it would be easier to read if there was space between the lines, thanks for letting me know. As for the code It was way too much for the comment box to handle, I hard code a lot its easier for me. I tried the code tag and it said that It exceeded the character limit.
When the code is too large, just attach the *.ino file rather than posting the code inline.
Using the method I showed, your code will be a lot shorter. I hope it helped.
Ok here's the whole code, and your advice was helpful. I just don't understand the last few lines
char fullSongName[20]; // Make this array big enough to hold the longest song name plus one extra char for the terminating zero.
sprintf_P(fullSongName, PSTR("music%d.mp3"), songNum); // Build the name.
mp3playerproto.ino (15.6 KB)
This is the buffer that the full song name will be put together in:-
char fullSongName[20];
A C string is always terminated in a '\0' character. 'sprintf()' adds it automatically. An extra character must be allocated in the buffer to allow room for this "terminating nul" character.
This prints formatted text to a C string:-
sprintf_P(fullSongName, PSTR("music%d.mp3"), songNum);
The normal version is 'sprintf()', and stores the format string in RAM, which is very limited in an AVR micro. The 'sprintf_P()' version stores the character string in program memory instead, to avoid wasting precious RAM.
The 'format string' is this part:- "music%d.mp3"
This is a 'format specifier' denoting an integer value:- %d
The %d in the format string is replaced by the integer value passed as the third parameter to sprintf_P(), and the result is placed into the buffer passed as the first parameter, 'fullSongName' in this case.
There are a range of format specifiers, %s for string, %d or %i for integers, %u for unsigned int, %c for char, and a number of others.
Here's a good reference to 'sprintf()':-sprintf
And this reference to 'printf()' has a list of allowed format specifiers and related modifiers, flags etc:- printf
if(songSequence = 0)
A further clue. Is that how you test whether 2 things are equal in C++ ?
AWOL:
if(songSequence = 0){
Always false.
if(songSequence = 1){
Always true.
Oops.
I missed that. (But only 23 times.
)
My excuse is that those lines would no longer be needed. 