0

HALP! Wave Shield n00b issue
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

HALP! Wave Shield n00b issue

by hughie_san on Fri Nov 30, 2018 2:53 am

Looking for help with what I assume is quite a simple wave shield function but I am a complete newbie to coding, arduino and the wave shield. I am trying to create a simple 'chant box', basically a Digital Audio Player with some specific functionality. I would like to be able to play and loop an individual .wav file, then skip to the next file stored, all action triggered by a single button.

I have been able to compile code from the Adafruit examples and some other users online to get close. I am able to play the individual files using a button on digital input 8 and successfully skip through the tracks in a consecutive loop. This is great, however the main issue is that I cannot get the file currently playing to continue to loop once it has begun playing. At the moment the file will play for it's full duration then stop, unless I use the button to skip to the next track obviously. I understand that in one of the adafruit examples the 'pressed[0]' function achieves this command. However I am only using a single button so if I use the 'pressed[0]' command in the code I have compiled currently I will just rapidly cycle through songs. Hence why I am using the 'justpressed[0].

Can I define this function else where? In other words a global function that defines playfile as something like 'loop file until told otherwise'? Please excuse my lack of knowledge here, I have no idea what I'm doing lol but need to complete this project for a unique application... THANK YOU IN ADVANCE.

Code: Select all | TOGGLE FULL SIZE
#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

int currentSongcount = 0;    // Keeps track of which song to play each time the button is pressed
int previousSongcount = 0; // previous song count
int maxSongcount = 3;        // Total number of songs on the SD card

#define DEBOUNCE 5  // button debouncer

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {8};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'pressed' (the current state
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

// 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() {
  byte i;
 
  // set up serial port
  Serial.begin(9600);
  putstring_nl("WaveHC with ");
  Serial.print(NUMBUTTONS, DEC);
  putstring_nl("buttons");
 
  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);

   //button
  pinMode(8, INPUT_PULLUP);
 
  // pin13 LED
  pinMode(13, OUTPUT);
 
  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
 
  //  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!");
 
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;


}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(buttons[index]);   // read the button
   
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
   
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  byte i;
  static byte playing = -1;

    //is the button pushed?
  int pushButton = digitalRead(buttons[0]);
  if (pushButton == LOW)
  {
    Serial.println("BPRESSED");
  } else {
    Serial.println("BNOTPRESSED");
  }
 
  if (justpressed[0]) {

    currentSongcount == currentSongcount++;
    Serial.println(currentSongcount);

    if (currentSongcount == 1) {
      if (playing != 1) {
      playing = 1;
      playfile("1.wav");
      }
    }
    else if (currentSongcount == 2)  {
      if (playing != 2) {
      playing = 2;
      playfile("2.wav");
      }
    }
    else if (currentSongcount == 3)  {
      if (playing != 3) {
      playing = 3;
      playfile("3.wav");
      }
  }

  if (! wave.isplaying) {
    playing = -1;
  }

  if (currentSongcount == maxSongcount)
    currentSongcount = 0;
    justpressed[0] = 0;
}

}


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

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 3:05 am

Any help here would be SERIOUSLY appreciated. Thanks guys.

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 7:42 am

Replace your loop() function with something like this:

Code: Select all | TOGGLE FULL SIZE
void loop()
{
   byte i;
   static byte playing = -1;

   //is the button pushed?
   int pushButton = digitalRead(buttons[0]);
   if (pushButton == LOW)
   {
      Serial.println("BPRESSED");
   }
   else
   {
      Serial.println("BNOTPRESSED");
   }

   if (justpressed[0])
   {
      currentSongcount == currentSongcount++;
      Serial.println(currentSongcount);
   }
   if (! wave.isplaying)
   {
      if (currentSongcount == 1)
      {
         if (playing != 1)
         {
            playing = 1;
            playfile("1.wav");
         }
      }
      else if (currentSongcount == 2) 
      {
         if (playing != 2)
         {
            playing = 2;
            playfile("2.wav");
         }
      }
      else if (currentSongcount == 3) 
      {
         if (playing != 3)
         {
            playing = 3;
            playfile("3.wav");
         }
      }
   }

   if (currentSongcount == maxSongcount)
   currentSongcount = 0;
   justpressed[0] = 0;
}

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 9:30 am

Hi, thank you so much for your response. Unfortunately this code hasn't worked. It's playing files but not looping the file, also I am no longer able to interrupt the current song playing to skip to the next track with your code. I noticed on the serial monitor that when I try to skip track whilst a file is playing it prints the current song count but I'm unable to actually skip tracks until the file has finished playing.

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 9:39 am

Try this. I eliminated some of the extraneous logic and variables.
Code: Select all | TOGGLE FULL SIZE
void loop()
{
   if (justpressed[0])
   {
      currentSongcount == currentSongcount++;
      Serial.println(currentSongcount);
   }
   if ((justpressed[0]) ||(! wave.isplaying))
   {
      if (currentSongcount == 1)
      {
         playfile("1.wav");
      }
      else if (currentSongcount == 2) 
      {
         playfile("2.wav");
      }
      else if (currentSongcount == 3) 
      {
         playfile("3.wav");
      }
   }

   if (currentSongcount == maxSongcount)
   currentSongcount = 0;
   justpressed[0] = 0;
}


adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 10:17 am

Wow thanks again for the prompt response. I believe this condensed version is very close but there are still flaws unfortunately :(

.wav number 1 and 2 will both play and remain looping while button is pressed but .wav number 3 will only play and not loop. Also, the button is a little bit inconsistent. Sometimes it works flawlessly skipping through all three tracks and at other times it just won't register an input or it will repeat the file currently playing. If it makes sense to have a second button on one of the spare analog or digital pins (so one for play and one for skip maybe?) I would be happy to incorporate this in to the design.

Thank you again for your assistance here, I've been at a dead end with this for a while now and this help is really appreciated.

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 10:26 am

This logic is what keeps #3 from looping:

Code: Select all | TOGGLE FULL SIZE
   if (currentSongcount == maxSongcount)
   currentSongcount = 0;


This should fix it:

Code: Select all | TOGGLE FULL SIZE
   if (currentSongcount > maxSongcount)
   {
      currentSongcount = 0;
   }


The button logic also looks more complicated than it needs to be. But best not to make too many changes at once.

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 10:35 am

OK so tested this and now it's cycling rapidly through the currentsong function. I open the serial monitor and it's just rapidly printing
1
2
3
4
1
2
3
4

Yes, oddly it is now also printing a '4'?

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 10:54 am

Are you sure you replaced just those two lines?

You still need this one at the end:
Code: Select all | TOGGLE FULL SIZE
   justpressed[0] = 0;

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 11:13 am

Ahh, mi scusi! Yes, as you suspected I had done that, whoops.

OK great, so tracks are all looping correctly. The only issue remaining is the inconsistencies. The button input doesn't always seem to register, sometimes I have to press it multiple times to get it to switch tracks. The serial monitor is also still registering a '4' at the end of the cycle but I assume maybe this is necessary to get .wav number 3 to loop?

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 11:23 am

See if this helps:

Code: Select all | TOGGLE FULL SIZE
void loop()
{
   if ((justpressed[0]) ||(! wave.isplaying))
   {
      if (justpressed[0])
      {
         currentSongcount == currentSongcount++;
         if (currentSongcount == maxSongcount)
         {
            currentSongcount = 0;
         }
         Serial.println(currentSongcount);
         delay(20);  // 20 ms of debounce
         justpressed[0] = 0;
      }

      if (currentSongcount == 1)
      {
         playfile("1.wav");
      }
      else if (currentSongcount == 2) 
      {
         playfile("2.wav");
      }
      else if (currentSongcount == 3) 
      {
         playfile("3.wav");
      }
   }
}

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 11:37 am

Very close it seems! The button is consistent now (with each press I am seeing a corresponding result in the serial monitor), however the 'currentsongcount' is reading as follows in the serial monitor.

1
2
0
1
2
0.....

And thus it is only playing 1.wav, 2.wav then returning back to 1.wav

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 12:04 pm

Sorry, lost the previous change to this line:

Code: Select all | TOGGLE FULL SIZE
if (currentSongcount == maxSongcount)


Should be
Code: Select all | TOGGLE FULL SIZE
if (currentSongcount > maxSongcount)

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: HALP! Wave Shield n00b issue

by hughie_san on Fri Dec 07, 2018 12:18 pm

It works! That's wicked, thank you SO MUCH :) It required a double press at the end of the sequence to return to the beginning as the code was as follows:

Code: Select all | TOGGLE FULL SIZE
currentSongcount == currentSongcount++;
         if (currentSongcount > maxSongcount)
         {
            currentSongcount = 0;
         }


but I changed it to this and it works :)

Code: Select all | TOGGLE FULL SIZE
currentSongcount == currentSongcount++;
         if (currentSongcount > maxSongcount)
         {
            currentSongcount = 1;
         }


Could you possible highlight the line that is responsible for looping playback of an individual file?

hughie_san
 
Posts: 17
Joined: Fri Nov 30, 2018 2:21 am

Re: HALP! Wave Shield n00b issue

by adafruit_support_bill on Fri Dec 07, 2018 12:27 pm

Could you possible highlight the line that is responsible for looping playback of an individual file?

There is no single line.

This line checks to see if:
(a) the button was pressed so we need to play the next track
OR
(b) there is nothing playing at the moment, so we may need to re-start the current track.

Code: Select all | TOGGLE FULL SIZE
   if ((justpressed[0]) ||(! wave.isplaying))
   {


These lines (which are inside the scope of the 'if' statement above). check to see if the current track is 1, 2 or 3 and starts (re)playing it.
Code: Select all | TOGGLE FULL SIZE
      if (currentSongcount == 1)
      {
         playfile("1.wav");
      }
      else if (currentSongcount == 2) 
      {
         playfile("2.wav");
      }
      else if (currentSongcount == 3) 
      {
         playfile("3.wav");
      }

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.