Howdy all,
I have been struggling for about a week on figuring out a simple .wav player using an arduino uno, arduino shield v1.1 and a MX9744 amp. The only example that is provided is a .wav player that searches the entire sd card and plays each .wav file found, the sketch is also handling all kinds of error testing and serial.print commands. For my application most if not all of this is not necessary.
I simply want to load and play the only .wav file named "Moon.wav", on a FAT32 partitioned SD card that is stored in the root directory.
My code is edited from Ladyada's code. My project doesn't require any reporting to serial or need any error checks. Basically, I just want the simplest code to play "moon.wav". From my attached code, it appears to me that the 'if' statements in the void setup are all just verifying that is expression is true. i have tried to remove the if statements that are doing the error checking, as soon as i do that i get this error "Couldnt open file "Moon.wav". I am hoping this can be accomplished. The current code is 91 lines, and takes up a large junk of the arduinos ram
Thank you!!
#include <FatReader.h>
#include <SdReader.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
void setup() {
Serial.begin(9600);
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while (1); // then 'halt' - do nothing!
}
// Now we will look for a FAT partition!
if (!vol.init(card, 1)) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while (1); // then 'halt' - do nothing!
}
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while (1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
Serial.println("Ready!!");
//delay(5000);
}
void loop() {
playcomplete("Moon.wav");
while (wave.isplaying) {
}
wave.stop();
}
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
while (1);
}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// do nothing while its playing
}
Serial.println("Done");
//delay(60000);
// now its done playing
wave.stop();
}
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();
}