Go Down

Topic: Adafruit Wave shield button press song (Read 978 times) previous topic - next topic

danieljay

So I purchased the wave shield. I have a simple (well...conceptually simple) action I want and that is:

if a button is on HIGH, pause playback of audio file. if its low, continue playing.

I have the shield playing back a song in a loop fine. I also interfaced a button to the wave shield and tested a basic button sketch which works fine and this concludes their are not any hardware issues.

My problem is nothing happens when I use my code of a basic modified sketch. it compiles, and just continuously plays back an audio file without any  regard for the switch. (made sure pin was correct in code). I have a testor LED that just very dimmly flickers on, and does nothing. As I said, I tested the button with a simple button state sketch and it works fine.

help. my code below:

Code: [Select]
/*
* This example plays every .WAV file it finds on the SD card in a loop
*/
#include <WaveHC.h>
#include <WaveUtil.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 volumes root directory
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads


/*
* Define macro to put error messages in flash memory
*/
#define error(msg) error_P(PSTR(msg))

const int buttonPin = 8;
const int ledPin =  13;

int buttonState = 0;

// Function definitions (we define them here, but the code is below)
void play(FatReader &dir);

//////////////////////////////////// SETUP
void setup() {
// Serial.begin(9600);
  // set up Serial library at 9600 bps for debugging
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);


  putstring_nl("\nWave test!");  // say we woke up!
 
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
// Serial.println(FreeRam());

  //  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!) 
    error("Card init. failed!");  // Something went wrong, lets print out why
  }
 
  // 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  :(
    error("No valid FAT partition!");  // Something went wrong, lets print out why
  }
 
  // 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)) {
    error("Can't open root dir!");      // Something went wrong,
  }
 
  // Whew! We got past the tough parts.
  putstring_nl("Files found (* = fragmented):");

  // Print out all of the files in all the directories.
  root.ls(LS_R | LS_FLAG_FRAGMENTED);
}

//////////////////////////////////// LOOP
void loop() {
 
   buttonState = digitalRead(buttonPin);
 
 
  root.rewind();
  play(root);

while(wave.isplaying){
   if(buttonState==HIGH){
  // wave.pause();
   digitalWrite(ledPin, HIGH); 
//   //Serial.println("HIGH");
  }
   else{
      //wave.resume();
      digitalWrite(ledPin, LOW); 
//    // Serial.println("LOW");
    }
  }
 
//while(wave.isplaying){
 

   
   

     

//}

/////////////////////////////////// HELPERS
/*
* print error message and halt
*/
}




void error_P(const char *str) {
  PgmPrint("Error: ");
  SerialPrint_P(str);
  sdErrorCheck();
  while(1);
}
/*
* print error message and halt if SD I/O error, great for debugging!
*/
void sdErrorCheck(void) {
  if (!card.errorCode()) return;
  PgmPrint("\r\nSD I/O error: ");
  //Serial.print(card.errorCode(), HEX);
  PgmPrint(", ");
  //Serial.println(card.errorData(), HEX);
  while(1);
}
/*
* play recursively - possible stack overflow if subdirectories too nested
*/
void play(FatReader &dir) {
  FatReader file;
  while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
 
    // Skip it if not a subdirectory and not a .WAV file
    if (!DIR_IS_SUBDIR(dirBuf)
         && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
      continue;
    }

   // Serial.println();            // clear out a new line
   
    for (uint8_t i = 0; i < dirLevel; i++) {
      // Serial.write(' ');       // this is for prettyprinting, put spaces in front
    }
    if (!file.open(vol, dirBuf)) {        // open the file in the directory
      error("file.open failed");          // something went wrong
    }
   
    if (file.isDir()) {                   // check if we opened a new directory
      putstring("Subdir: ");
      printEntryName(dirBuf);
     // Serial.println();
      dirLevel += 2;                      // add more spaces
      // play files in subdirectory
      play(file);                         // recursive!
      dirLevel -= 2;   
    }
    else {
      // Aha! we found a file that isnt a directory
      putstring("Playing ");
      printEntryName(dirBuf);              // print it out
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
        putstring(" Not a valid WAV");     // ok skip it
      } else {
       // Serial.println();                  // Hooray it IS a WAV proper!
        wave.play();                       // make some noise!
       
        uint8_t n = 0;
        while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
          putstring(".");
          if (!(++n % 32))//Serial.println();
          delay(100);
        }       
        sdErrorCheck();                    // everything OK?
        // if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
      }
    }
  }
}

PaulS

I know that this has been suggested before, but I'm going to say it again. Put every { on a new line. Use Tools + Auto Format to properly indent your code.

Then, it should be pretty obvious what the problem is. Where do you read the switch state? Where do you care about the switch state? In the loop where you care, why do you not read?

Quick5pnt0

Just looking through your code really quickly, you have wave.pause(); commented out.

danieljay

Thanks guys. the wave pause has been uncommented, moved, and adjusted every which way and I can't find a working solution.


Quote
Where do you read the switch state? Where do you care about the switch state?


am I not doing this with,    "buttonState = digitalRead(buttonPin);" ?

PaulS

Quote
am I not doing this with,    "buttonState = digitalRead(buttonPin);" ?

You are. Once.

Where do you care? In the while loop. Where do you read? Before the while loop.

while(wave.isplaying){
   buttonState = digitalRead(buttonPin);
   if(buttonState==HIGH){

danieljay

thanks for the suggestion, PaulS.

I have revised my loop as follows:

Code: [Select]
void loop() {
 
   buttonState = digitalRead(buttonPin);
 
 

// play(root);

root.rewind();
  play(root);

while(wave.isplaying){
  buttonState = digitalRead(buttonPin);
   if(buttonState==HIGH){
  wave.pause();
   digitalWrite(ledPin, HIGH); 
Serial.println("HIGH");
  }
   else{
    wave.resume();
      digitalWrite(ledPin, LOW); 
Serial.println("LOW");
    }
  }

}


It seems the while loop is never gone into. The serial messages are never printed.

wildbill

Your play function has a while loop testing wave.isplaying. It won't return until it is false, so your similar setup in loop will always be false and thus your prints are never executed.

danieljay

Thanks for the suggestion. I am not quite clear on what you mean. could you show me with a code example?

wildbill

In the play function, I see this:
Code: [Select]
       
// Serial.println();                  // Hooray it IS a WAV proper!
wave.play();                       // make some noise!
       
uint8_t n = 0;
while (wave.isplaying)
  {// playing occurs in interrupts, so we print dots in realtime
  putstring(".");
  if (!(++n % 32))//Serial.println();
    delay(100);
  }       


So when a valid wav file has been found, this bit plays it. Because  of the while loop, it will only exit once the file has been played and wave.isplaying is false. Your code in loop then immediately tests the same wave.isplaying status and apparently you expect it to be true in some cases. It won't be though because play didn't exit until it was false.

danieljay


So I tried your suggestion as this being the only chunk in the main loop with the buttonstate added. nothing plays.


Code: [Select]
wave.play();                       // make some noise!
       
uint8_t n = 0;
while (wave.isplaying)
  {// playing occurs in interrupts, so we print dots in realtime
buttonState = digitalRead(buttonPin);

putstring(".");
  if (!(++n % 32) && buttonState==HIGH){//Serial.println();
    //delay(100);
  wave.pause();
  }
  else{
    wave.resume();


}   

}

ash901226

Code: [Select]
 
  buttonState = digitalRead(buttonPin);
 
 
 root.rewind();
 play(root);

while(wave.isplaying){
  if(buttonState==HIGH){
 // wave.pause();
  digitalWrite(ledPin, HIGH);  
//   //Serial.println("HIGH");
 }
  else{
     //wave.resume();
     digitalWrite(ledPin, LOW);  
//    // Serial.println("LOW");
   }
 }



actually your have not define a micro....
how about you do this at the top
#define buttonState digitalRead(buttonPin);
this way every time you write buttonState it mean it will read the current state of the button pin

what you did here in perticular is
Code: [Select]
buttonState = digitalRead(buttonPin);
 
 
 root.rewind();
 play(root);

while(wave.isplaying){
  if(buttonState==HIGH){


your only using the state that was read outside the while loop so if b4 the while loop happen the state of the button state is low, then inside the while loop ,that if will never be true.

ash901226

use the program that you use in reply 5 and but change this
Code: [Select]
if(buttonState==HIGH){
to this
Code: [Select]
if(digitalRead(buttonPin)==HIGH){


wildbill

You're making life harder for yourself using that example code - it's designed to loop through every wav file on your SD card. There is a simpler example on the ladyada site (where I assume you got your code): http://www.ladyada.net/make/waveshield/libraryhcplay6.html. Try and get the playfile function working in your code instead of play. Then you should finally see your serial prints at least.

Go Up