0

I have an Arduino Uno V3. The sketch I'm working on has a couple of global vars in which a file within a directory is stored which I want to playback on the Adafruit Music Maker shield. All is fine, as long as I just want to play the file which is referenced in the vars on instantiation, but when I call my function where I need to modify these char arrays, my program or the Arduino hangs or crashes.

These are my variables:

// mp3 player variables
boolean plrStartPlaying = false;              // Is set to true in case an nfc tag is present with information on tracks to play
char plrCurrentFile[13] = "track001.mp3";     // which track is the player playing?
char plrCurrentFolder[9] = "system00";        // from which directory is the player playing?
char filename[23] = "/system00/track001.mp3"; // path and filename of the track to play
byte firstTrackToPlay = 1;                    // the track number as received from the tag
char curTrackCharNumber[4] = "001";

And this is my function I call to modify them:

// stores the file to play in the global var filename - it is created from the gloal vars plrCurrentFolder and plrCurrentFile
boolean createFileNameToPlay(byte trackNo) {
  // fill global var curTrackCharNumber with provided trackNo - we need it next to create the global var plrCurrentFile
  sprintf(curTrackCharNumber, "%03d", trackNo);

  // create the name of the track to play from the curTrack
  strcpy(plrCurrentFile[0], '\0');
  strcat(plrCurrentFile, "track");
  strcat(plrCurrentFile, curTrackCharNumber);
  //strcat(plrCurrentFile, sprintf(plrCurrentFile, "%03d", trackNo));
  strcat(plrCurrentFile, ".mp3");

  // create the filename of the track to play, including path, so we can feed it to the music player
  strcpy(filename[0], '\0');
  strcat(filename, "/");
  strcat(filename, plrCurrentFolder);
  strcat(filename, "/");
  strcat(filename, plrCurrentFile);
  if (SD.exists(filename)) {
    return (true);
  } else {
    return (false);
  }
}

As said it does not work - I checked by commenting out all strcat() and strcpy() calls in the function and then my program works just fine. But if the code is active, it actually causes my Arduino to hang or the program to crash. Whatever the cause, the effect is that my program won't advance once this function is called and after a certain time, the Arduino resets.

Can someone explain to me why this happens?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
siliconchris
  • 613
  • 2
  • 9
  • 22

1 Answers1

2

You try to use strcpy() to copy a character to a character, not a character pointer to a character pointer — and the problem occurs (at least) twice:

strcpy(plrCurrentFile[0], '\0');
…
strcpy(filename[0], '\0');

That should be generating compiler errors; both arguments should be char *, but neither is a char *. Write either:

plrCurrentFile[0] = '\0';
…
filename[0] = '\0';

or:

strcpy(plrCurrentFile, "");
…
strcpy(filename, "");

Why are you not heeding compiler warnings? If you're not getting at least warnings, why not? Find the options necessary to create such warnings and preferably make them into errors. For example, with GCC, consider:

gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …

That's my base set of options; I don't run code that doesn't compile cleanly with those options. I sometimes add more.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278