The attached code was working perfectly and then it stopped playing the music. I had added a couple variables and some code inside the loop(). Even thought the IDE said SRAM was 1785 out of 2000 (UNO) I thought I might have gone over the limit so I commented out the changes and reduced the SRAM but it still didn't work. If I load the daphc example from the WaveHC library, it plays all of the sound files perfectly. With my code, I now get a consistent "Couldn't open file kids2.wav" or any other file I try. To the best of my knowledge I haven't touched the WaveHC code. Can anyone see the discrepancy? I'm 70 and need all the help I can get.
// this is the "final" test version that is working well. remote can trigger a song.
// slave 3 slave 3
//#include <FatReader.h>
//#include <SdReader.h>
//#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <Wire.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
//*********** I2C variables *************
byte cb[100]; // [0] to [49] are incoming from master, [50] to [99] are outgoing to master
byte cbb[25];
byte cbsend[25];
byte tled=A0;
byte rled=A1;
int swt=0;
int swr=0;
int insw=0;
int outsw=0;
byte slavenum=3;
byte jsw=0;
//**************end I2C variables *****************
//*********** I2C variables definitions *************
byte& mode=cb[0];
byte& function=cb[1];
byte& action=cb[2];
byte& sysmode=cb[3];
byte& perfmode=cb[4];
byte& testmode=cb[5];
byte& songnum=cb[10];// index # of song to be played
//**************end I2C variables definitions *****************
//***************** system variables **********************
byte oldaction=0;
byte oldsongnum=0;
byte song=0;
//******************* end system variables *****************************
//this handy function will return the number of bytes currently free in RAM, great for debugging!
// int freeRam(void)
// {
// extern int __bss_end;
// extern int *__brkval;
// int free_memory;
// if((int)__brkval == 0) {
// free_memory = ((int)&free_memory) - ((int)&__bss_end);
// }
// else {
// free_memory = ((int)&free_memory) - ((int)__brkval);
// }
// return free_memory;
// }
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);
}
//******************** begin SETUP *******************************
void setup() {
// set up serial port
Serial.begin(57600);
// putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
// Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(tled, OUTPUT);
pinMode(rled, OUTPUT);
Wire.begin(slavenum);
Wire.onReceive(cbin);
Wire.onRequest(cbout);
for(int jk=0;jk<=24;jk++)cbb[jk]=0;
for(int jj=0;jj<=99;jj++)
{
cb[jj]=0;
}
cb[74]=24;
cb[99]=49;
for (int jj=0;jj<=24;jj++)
{
cbsend[jj]=0;
}
digitalWrite(rled,LOW);
digitalWrite(tled,LOW);
//************** end I2C setup ************************
// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
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!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // 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!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
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!");
}
//************************** end SETUP ******************************
//********************** loop begins *******************************
void loop() {
// Serial.print(mode);
// Serial.print(" ");
// Serial.print(function);
// Serial.print(" ");
// Serial.print(action);
// Serial.print(" ");
// Serial.print(sysmode);
// Serial.print(" ");
// Serial.print(perfmode);
// Serial.print(" ");
// Serial.print(testmode);
// Serial.print(" ");
// Serial.println(songnum);
sysmode=2;
songnum=3;
switch(sysmode){
case 1:
wave.stop();
break;
case 2:
if(songnum == 0){wave.stop(); return;}
if(songnum != 0){
if(songnum == 99) {wave.stop(); return;}
if(songnum == oldsongnum && wave.isplaying)return;
if(songnum != oldsongnum){
wave.stop();
oldsongnum=songnum;
//Serial.print("songnum = ");
//Serial.println(songnum);
switch (songnum) {
case 1:
playfile("atmos.wav");
break;
case 2:
playfile("melbel.wav");
break;
case 3:
playfile("kids2.wav");
break;
case 4:
playfile("kids3.wav");
break;
case 5:
playfile("spacy1.wav");
break;
case 6:
playfile("mspolly.WAV");
break;
}
}
}
break;
case 3:
if(mode == 2 && function == 4) {
if(action==0)return;
if(action==99) {wave.stop(); return; }
if(action==oldaction && wave.isplaying) return;
if(action != oldaction) {
wave.stop();
oldaction=action;
// Serial.print("action - ");
// Serial.println(action);
switch (action) {
case 1:
playfile("atmos.wav");
break;
case 2:
playfile("melbel.wav");
break;
case 3:
playfile("kids2.wav");
break;
case 4:
playfile("kids3.wav");
break;
case 5:
playfile("spacy1.wav");
break;
case 6:
playfile("mspolly.WAV");
break;
}
}
}
break;
}
delay(500);
}
//******************** loop ends *****************************************
// 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
}
// 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();
}
//****IN ******* IN *****************
void cbin (int x){
if(swr==0)digitalWrite(rled,HIGH);
for(int jj=0;jj<=24;jj++)cbb[jj]=Wire.read();
for(int jj=0;jj<=24;jj++)cb[(cbb[24]-24)+jj]= cbb[jj];
if(swr==5)digitalWrite(rled,LOW);
if(swr==8)swr=-1;
swr++;
}
//************** end IN ***************************
//*****OUT*********OUT****************
void cbout(){
if(swt==0)digitalWrite(tled,HIGH);
if(outsw==2)outsw=0;
for(int jj=0;jj<=24;jj++)cbsend[jj]= cb[jj+50+(outsw*25)];
Wire.write(cbsend,25);
outsw++;
if(swt==5)digitalWrite(tled,LOW);
if(swt==8)swt=-1;
swt++;
}
//******************* end OUT *********************