Wave Shield Cannot Find Files on SD Card

I am trying to create a keyboard that plays back piano notes using the wave shield and 6 buttons, on pins A0 - A5. Upon pressing a button, a single note file should be played once through. Pressing more than one note plays a different wav file with that particular chord. Notes can interrupt each other and begin playing a new wav file.

This example is adapted from #7 under “6 Buttons, 6 Sounds, Multiple Possibilities” on the Wave Shield Examples page found here.

The Wave Shield Library can be found here.

I modified the loop part of the sketch to create a string of the file name to call, then turned that string into a char array so the playfile() function can read it. The filenames of the wav files are in the format “P_######.wav”, where each # represents a particular note, such that # = 1 if the note is played and # = 0 if the note is not being played. If buttons 0, 1, and 3 were pressed, it would play this file: “P_001011.wav”.

The sketch complies and loads onto the Arduino Uno successfully, but upon pressing the buttons, no notes are played. The monitor reads “Couldn’t open file P_001011.wav”, for whichever wav files are called. Before I modified the code within the loop function, the sketch ran perfectly, and the wave shield produced sound. I suspect the problem lies in this bit of code I changed, which I copied below, along with the playfile function.

(The entire code for this sketch is attached to the post.)

/////     L O O P   W /   C O N T R O L   C O D E     /////

void loop() {
  
  for (byte i = 0; i < 6; i++) {

    if (justpressed[i]) {
      String array[6];
      
      for (byte j = 0; j < 6; j++) {
        if (pressed[j]) {
          array[j] = "1";
        }
        else {
          array[j] = "0";
        }
      }
      
      String fileName = "P_" + array[0] + array[1] + array[2] + array[3] + array[4] + array[5] + ".wav";
      char name [12];
      for (int j = 0; j < 12; j++) {
        name[j] = char(fileName[j]);
      }
      
      playfile(name);
      justpressed[i] = 0;
    }
  }
}


/////     L O O P   H E L P E R S     /////

// THIS CODE WILL: play a full file from beginning to end with no pause, and stop it if a new button is pressed
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  // do nothing while its playing
  }
  // now its done playing
}

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name)) {
    putstring("Couldn't open file "); Serial.print(name); return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;
  }
  // ok time to play! start playback
  wave.play();
}

I appreciate any and all help! Thank you!

WaveShield_Attempt_4.txt (7.62 KB)

I modified the loop part of the sketch to create a string of the file name to call

No, you didn't. You create a String. That is NOT the same as a string.

And, actually, you created an array of Strings, each of which holds one character. That was pretty much useless. An array of chars would make a lot more sense.

Define an array of appropriate size:

char fileName[] = "P_xxxxxx.wav";

Replace the i+2 element in the array with '0' or '1' if the ith pin is LOW or HIGH. It would actually be possible to assign the value:

   fileName[i+2] = digitalRead(A0+i) + '0';

in a for loop iterating from 0 while less than 6.