Wave Shield Button pause
Moderators: adafruit_support_bill, adafruit

Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 2:22 am

So I had received my Wave Shield and everything works great....almost.

I had used the daphc sketch without any issues. I then wanted to implement a switch into the sketch to control playback/testor LED.

nothing. I took my button configuration and tested a basic button state sketch in Arduino with success, so that eliminated a hardware issue. Here is my modified sketch of the daphc sketch with only an addition of button state/ led . Help greatly appreciated!


Code: Select all | TOGGLE FULL SIZE
/*
 * 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);


 
//////////////////////Here is what is giving me issues. the LED simply flickers lightly, then turns off. ////////////////////////////////
while(wave.isplaying){
 if(buttonState==HIGH){
   wave.pause();
   digitalWrite(ledPin, HIGH); 
//   //Serial.println("HIGH");
  }
   else{
      wave.resume();
      digitalWrite(ledPin, LOW); 
//    // Serial.println("LOW");
    }
}
     

}
////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// 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
      }
    }
  }
}
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am

Re: Wave Shield Button pause

by adafruit_support_bill on Tue Jan 29, 2013 6:19 am

What happens to the playback?
This bit of logic looks problematic. When you press the button, you pause the playback. Doesn't that make you exit the 'while' loop.
Code: Select all | TOGGLE FULL SIZE
    while(wave.isplaying){
    if(buttonState==HIGH){
       wave.pause();
       digitalWrite(ledPin, HIGH);
    //   //Serial.println("HIGH");
      }
       else{
          wave.resume();
          digitalWrite(ledPin, LOW);
    //    // Serial.println("LOW");
        }
    }
User avatar
adafruit_support_bill
 
Posts: 29657
Joined: Sat Feb 07, 2009 9:11 am

Re: Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 10:21 am

The audio continues playing. I am not sure what exactly I need to do. Have been at this all night
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am

Re: Wave Shield Button pause

by adafruit_support_bill on Tue Jan 29, 2013 10:28 am

What do you see in the serial monitor if you uncomment those Serial.println statements?
User avatar
adafruit_support_bill
 
Posts: 29657
Joined: Sat Feb 07, 2009 9:11 am

Re: Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 10:31 am

nothing.
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am

Re: Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 10:41 am

I have tried the button examples, too and they seem to not work when adjusting the correct settings. I am sure this is much more simple then what I am making it.
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am

Re: Wave Shield Button pause

by adafruit_support_bill on Tue Jan 29, 2013 10:51 am

nothing.

With the wave file playing, you should see at least one HIGH or LOW.
User avatar
adafruit_support_bill
 
Posts: 29657
Joined: Sat Feb 07, 2009 9:11 am

Re: Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 11:00 am

I do not see a HIGH or a LOW. it does not even go into the loop from what it looks like. I am using an Arduino Uno, but that shouldnt be an issue since your site says it supports 328 atmel chips.

Any suggestions?
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am

Re: Wave Shield Button pause

by adafruit_support_bill on Tue Jan 29, 2013 11:45 am

What about all the messages in setup() and play() ? It must be printing something if it is playing the sound.
User avatar
adafruit_support_bill
 
Posts: 29657
Joined: Sat Feb 07, 2009 9:11 am

Re: Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 11:48 am

Yes, it shows the Serial.println commands shown all throughout the sketch. ok. I am not sure how this is relevant. is their not a quick suggestion in code structure to simply get a button to pause and resume play?
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am

Re: Wave Shield Button pause

by danbertner on Tue Jan 29, 2013 11:51 am

GOT IT.

Code: Select all | TOGGLE FULL SIZE
     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
          buttonState = digitalRead(buttonPin);
         
          putstring(".");
          if (!(++n % 32))Serial.println();
          delay(100);
       if (buttonState==HIGH){
         wave.pause();
       }else{
         wave.resume();
       }
     
    }       
        sdErrorCheck();
danbertner
 
Posts: 22
Joined: Sun Jan 20, 2013 7:58 am