Pages: [1]   Go Down
Author Topic: SdSimpleAudio Question  (Read 1002 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im sorry for not posting this in the thread started by TuttTutt, but I needed the exposure.  I have a problem.  I can get the files to play when they are written into the basic example file, but when I change my file, that is where I get into trouble. 

I'd like to be able to play files from Sd and output them through PWM with the new library.  I just cannot for the life of me figure out how to make the buttons work.  It is driving me crazy.  I have used the WaveHC files as a starting point and also those from SimpleSdAudio.  I am stuck...

Ill upload the code.
Code:
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <SimpleSDAudio.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

#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[] = {14, 15, 16, 17, 18, 19};
// 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");
 
  if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO)) {
    while(1); // Error while initialization of SD card -> stop.
  }
 
  /*
  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);
 
  // pin13 LED
  pinMode(13, OUTPUT);
 
 */
  pinMode(10,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;
 
  if (justpressed[0]) {
    justpressed[0] = 1;
    SdPlay.setFile("green.afm");
    SdPlay.play();
  }
  else if (justpressed[1]) {
      justpressed[1] = 0;
      SdPlay.setFile("c22.afm");
      SdPlay.play();
  }
  else if (justpressed[2]) {
      justpressed[2] = 0;
      SdPlay.setFile("spoke2.afm");
      SdPlay.play();
  }
  else if (justpressed[3]) {
      justpressed[3] = 0;
      SdPlay.setFile("spoke3.afm");
      SdPlay.play();
  }
  else if (justpressed[4]) {
      justpressed[4] = 0;
      SdPlay.setFile("spoke4.afm");
      SdPlay.play();
  }
  else if (justpressed[5]) {
      justpressed[5] = 0;
      SdPlay.setFile("spoke12.afm");
      SdPlay.play();
   }
   // Let the worker work until playback is finished
  while(!SdPlay.isStopped()) {
    SdPlay.worker();
 }
}
 

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

thanks for the help.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8690
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (justpressed[0]) {
    justpressed[0] = 1;
    SdPlay.setFile("green.afm");
    SdPlay.play();
  }

I think you meant to do "justpressed[0] = 0;" like in the other cases.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For a start I would get rid of all that Wave and SDCard stuff. SimpleSdAudio has an optimised SD Card library built in.

With the other bits there you don't know what might be conflicting behind the scenes in the libraries.

Clean your code up first, or just start from scratch with SSDA and add in the button code to play the files.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I cleaned everything out that I think should not have been there.  But now, when I go to hit a button, nothing happens.  I have tried the generic pushbutton sketch in the examples and it's almost like the button sticks.  I guess that could be the problem.  I have other buttons, but they are not thru-hole.  I will have the try them tomorrow. 

Code:
Code:
#include <SimpleSDAudio.h>
#define DEBOUNCE 5                                  // button debouncer
void setup() {
  Serial.begin(9600);                                  //start serial comm.
  SdPlay.setSDCSPin(10);                            //Using SD shield, need to be 10.
  if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO)) {     //fullrate, mono output
    while(1); // Error while initialization of SD card -> stop.
  }
  pinMode(10,OUTPUT);   //needed for shield
  pinMode(14,INPUT);    //button I'm trying to give my human input to play file
  }
void loop() {
   int buttonState = digitalRead(14);      //so I can see output of buttonpress in serial monitor
   Serial.println(buttonState);            //ditto...continued.
   delay(2);                                       //time to collect "thoughts"
   if (14,LOW){ //This line makes the sketch play when set to HIGH whether hitting a button or
                 //not, and if I set it to LOW, it doesnt play at all.  I'm thoroughly confused,
                 //or maybe it is the lack of sleep and sustanance.  I think Ill sleep some more
                 //tomorrow.
    SdPlay.setFile("green.afm");        //play this file.
    SdPlay.play();                             //play it now.
   }
   // Let the worker work until playback is finished
  while(!SdPlay.isStopped()) {          //apparently this has to do work son!
    SdPlay.worker();                    //SdCard Hardhat zone.
 }
}

Thanks again community!  Sleepy time!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8690
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   if (14,LOW) { 

Did you mean to write:

Code:
   if (digitalRead(14) == LOW) {

The way you wrote it, leaving out the function call and operator, won't do a thing.  The expression "14,LOW" is equivalent to "LOW" which is equal to 0.  The statement "if (0)" is equivalent to "if (0 != 0)" so the body of the if statement will never be executed.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That really helps, and I knew that it was something really simple as changing the syntax of my code.  Thanks.  I thought the exact same thing when I woke up this morning.  I really need to do the notebook-by-the-bed thing.

It sure is nice to have people who want to help.  I really appreciate it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got my code to work, it might not be the most efficient, but it is my first working Arduino project.  It was a christmas present for my wife.  I have four buttons that will be playing different sound bytes of my talking that she can press that have been put into a monkey that we take turns hiding.  I hope she likes it. 

Here's the code.
Code:
#include <SimpleSDAudio.h>
#define DEBOUNCE 5  // button debouncer
  int buttonState = 0;
  int buttonPushCounter = 0;
  int lastButtonState = 0;
void setup() {
  Serial.begin(9600);  //start serial comm.
  Serial.println("Welcome to the monkey business! Please press a button.");
  Serial.println("I will be playing different sounds whenever you select a button.");
  SdPlay.setSDCSPin(10);   //Using SD shield, need to be 10.
  if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO)) {     //fullrate, mono output
    while(1); // Error while initialization of SD card -> stop.
  }
  pinMode(10,OUTPUT);   //needed for shield
  pinMode(14,INPUT);    //button I'm trying to give my human input to play file
  pinMode(15,INPUT);
  pinMode(16,INPUT);
  pinMode(17,INPUT);
  }
void loop() {
    digitalWrite(14, HIGH);
    if (digitalRead(14) == LOW) {                        //This will cause music to be played.
      SdPlay.setFile("spoke7.afm");                        //play this file.
      SdPlay.play();                                       //play it now.
                                                         // Let the worker work until playback is finished
        while(!SdPlay.isStopped()){                          //apparently this has to do work son!
          SdPlay.worker(); }                                  //SdCard Hardhat zone.
    }
    digitalWrite(15,HIGH);
    if (digitalRead(15) == LOW) {                        //This will cause music to be played.
      SdPlay.setFile("spoke8.afm");                        //play this file.
      SdPlay.play();                                       //play it now.
                                                         // Let the worker work until playback is finished
        while(!SdPlay.isStopped()){                          //apparently this has to do work son!
          SdPlay.worker(); }                                  //SdCard Hardhat zone.
    }
    digitalWrite(16,HIGH);
    if (digitalRead(16) == LOW) {                        //This will cause music to be played.
      SdPlay.setFile("spoke9.afm");                        //play this file.
      SdPlay.play();                                       //play it now.
                                                         // Let the worker work until playback is finished
        while(!SdPlay.isStopped()){                          //apparently this has to do work son!
          SdPlay.worker(); }                                  //SdCard Hardhat zone.
    }
    digitalWrite(17,HIGH);
    if (digitalRead(17) == LOW) {                        //This will cause music to be played.
      SdPlay.setFile("spoke10.afm");                        //play this file.
      SdPlay.play();                                       //play it now.
                                                         // Let the worker work until playback is finished
        while(!SdPlay.isStopped()){                          //apparently this has to do work son!
          SdPlay.worker(); }                                  //SdCard Hardhat zone.
    }
}

Thanks for the help.
Logged

UK
Offline Offline
Newbie
*
Karma: 2
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice work!  Thanks for sharing the example :-)

Out of curiosity, what did you use for a speaker and on which Arduino?  Was it very loud?  Would love to hear/see a youtube vid of the monkey if you can prise it away from the wife  :-)
Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1235
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wold also be interested in the audio final output...

I tested the initial lib posted by Tuttut.. but not the latest one (or any after the first revision)...


so Im curious as to what you did/used for the speaker output... just a 100uF cap?  or did you add in a low-pass filter of some kind?  or the addition of an amp as well?

Im hoping just the cap will be 'ok'..  (room is a severe problem for me on this latest project) 


thanks!
Logged


Pages: [1]   Go Up
Jump to: