Trying to use Wave Shield to play sounds from multiple presses of one button.

Hello,

I have been working with an adafruit wave shield recently and trying to modify the code that plays different wav files based on input from 6 different buttons. Below is the original sketch.

https://cdn-learn.adafruit.com/assets/assets/000/010/347/original/wavehc_play6interloop.pde.txt

What I would like to be able to do is modify the code so that instead of 6 different buttons playing 6 different sounds, the arduino uses one button and counts the button presses to play each sound. For example, no button presses plays the DO.WAV file on loop, then one button presses plays the RE.WAV file on loop, then the next press plays MI.WAV on loop, and so on...

This was my attempt but the wave shield plays nothing at all no matter how many button presses.

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.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

const int button = 7;

int counter = 0;
int buttonState = 0;
int lastButtonState  = 0;

// 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);
}

void setup() 
{
 // 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(button, INPUT);
  
// 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!
  }
  // 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!
  }
}




void loop() 
{

  buttonState = digitalRead(button); //read the push button and assign it's value to the buttonState variable
 
 if (buttonState != lastButtonState){ //if the buttonstate does not equal the last button state
   if (buttonState == HIGH) { //and if the button is currently being pressed
    counter++; //increase the counter
   
   }
 }

lastButtonState = buttonState;

 byte i;
  static byte playing = -1;
  
  if (counter == 0) {
    if (playing != 0) {
      playing = 0;
      playfile("DO.WAV");
    }
  }
  else if (counter == 1) {
    if (playing != 1) {
      playing = 1;
      playfile("RE.WAV");
    }
  }
  else if (counter == 2) {
    if (playing != 2) {
      playing = 2;
      playfile("MI.WAV");
    }  
  } 
  else if (counter == 3) {
    if (playing != 3) {
      playing = 3;
      playfile("FA.WAV");
    }
  } 
  else if (counter == 4) {
    if (playing != 4) {
      playing = 4;
      playfile("SO.WAV");
    }
  }
  else if (counter == 5) {
    if (playing != 5) {
      playing = 5;
      playfile("LA.WAV");
    }
  }

  if (! wave.isplaying) {
    playing = -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
  }
  // 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 understand that I'm very new to this so please try not to judge. I want to learn how to become much better at this but unfortunately I can't find much documentation on working with the wave shield so I'm left asking for help here.

Any help is greatly appreciated,

J

Go to the IDE and look under the menu File -> Examples -> 02.Basics -> StateChangeDetection.
There you will find example code to count button presses. Pass the result of the button press count to the bit that defines what file you are going to play.

Thank you for your quick response. I've looked at your suggestion and I believe I have already done that. I've made a very similar project with LEDs instead of playing sound from the wave shield and that worked fine. I'm still hearing nothing come through the wave shield with this code but I've tested it separately and known I have no hardware issues. Could you please tell me where in my code above I may have gone wrong?

Could you please tell me where in my code above I may have gone wrong?

Well it would be much better if you could find out yourself.

Use Serial.print to send debug messages to the Arduino's serial port.

By using this you can trace the progress of your code.

So things to check are:-
Is the code making it to the start of the loop function?
Is the code increasing the count as you press the button?
Is the fileplay being called up.

You can do all these with a serial print and it will guide as to where you are going wrong. This is an important skill in learning how to program. If you are still puzzled then get back to us with the results of these tests.

Will do. Thank you for your input.