code for button random & WaveShield

Could you please advise me about the program for Arduino WaveShield? I have assembled the kit and made the WaveShield run with WaveHC and the schetch found at ladyada.net works properly: I can control the files on my SDcard with six buttons, each playing one file.wav. For my project I'll need each button to play one file.wav that will be randomly extracted from a group of files.wav. I've read on the forum that it is possible to generate a random number and to use a switch statement , for example :

int songCount = 4; // based on the numbers of files in the group int songNumber = random (songCount+1) switch(songNumber) { case 0: playcomplete ("sound1.wav"); break; }

How can I combine this schetch with the schetch found at ladyada.net Or are there alternative solutions to this problem?

Looking forward to hearing from you.

How can I combine this schetch with the schetch found at ladyada.net

There is more than one sketch to be found on ladyada.net. If you posted a link to the specific sketch you'd like to have play random songs, instead of a specific song, we could help.

Also, do you want to play the same song every time a specific button is pressed, or a different song every time that button is pressed? If you want a different song every time a specific button is pressed, why do you need 6 buttons?

Thank you for your swift reply. I'll specify my question: I'ld like to create a combination of songs from 6 different countries, i.e. (Italian, German, English etc) that will be randomly chosen from the respective group. How could, for example, the "German button" recognize the group file of German songs and make a random selection? The sketch I'm using is from: http://www.ladyada.net/media/wavshield/daphc.pde Could you help me with this? Please take into consideration that this is my first project and I'm not an expert.

Are you able to create directories on the SD card?

Are you able to list those directories, and their contents, on the SD card?

The play() function takes as an argument a directory. It then finds all the directories in that directory, and plays all the song in that directory. It is a recursive function, so when it finds a directory, it calls itself again.

You need to change play() so that it is no longer recursive. Instead, you need to create an array of FatReader objects, and store the sub-directory instances in that array.

Then, you'll need to create another function, similar to play that populates an array of FatReader objects with the file objects that pass the !wave.create(file) test, as well as counting those objects.

Call the current play function in setup.

Then, in loop(), when the nth button is pressed, pass the nth object in the array of FatReader objects to the new function (the directory containing the songs).

In the new function, after populating the FatReader object array (with file objects that are mp3 files) and counting them, randomly select an index, and call wave.create with the FatReader array object at that position (the song file). Then, play that file.

Sorry PaulS,
I was checking the program and I realized that I gave you the wrong sketch.
Here’s the right one:

#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

#define DEBOUNCE 100  // button debouncer

// 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 up serial port
  Serial.begin(9600);
  putstring_nl("WaveHC with 6 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);
 
  // pin13 LED
  pinMode(13, OUTPUT);
 
  // enable pull-up resistors on switch pins (analog inputs)
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
  digitalWrite(17, HIGH);
  digitalWrite(18, HIGH);
  digitalWrite(19, 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!");
}

void loop() {
  //putstring(".");            // uncomment this to see if the loop isnt running
  switch (check_switches()) {
    case 1:
      playcomplete("1.WAV");
      break;
    case 2:
      playcomplete("2.WAV");
      break;
    case 3:
      playcomplete("3.WAV");
      break;
    case 4:
      playcomplete("4.WAV");
      break;
    case 5:
      playcomplete("5.WAV");
      break;
    case 6:
      playcomplete("6.WAV");
  }
}

byte check_switches()
{
  static byte previous[6];
  static long time[6];
  byte reading;
  byte pressed;
  byte index;
  pressed = 0;

  for (byte index = 0; index < 6; ++index) {
    reading = digitalRead(14 + index);
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch pressed
      time[index] = millis();
      pressed = index + 1;
      break;
    }
    previous[index] = reading;
  }
  // return switch number (1 - 6)
  return (pressed);
}


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

concerning your answer, theoretically I understood, but for me is really difficult, without one example, to put your advice into practice.
Can you suggest any place where I can find some of that ?
Thanks for your time and sorry again for the wrong sketch :-[

This sketch simply verifies that the card can be accessed, and then hard-codes names of files on the card to play.

The previous sketch really is a better starting point for what you want to do.

Copy the play function, and call the copy findDirs. Rename play to findFiles.

Create two global array of FatReader objects:

FatReader dirs[6]; // Since you have 6 buttons
FatReader mp3s[10]; // However many songs you might have

In the findDirs function, remove the code that recursively calls play. There is a function, isDir(), that tells whether the object on the SC card is a file or a directory. findDirs wants to ignore files, and store directories in the dirs array.

Then, in findFiles, ignore the directories, and find the files in the input directory. Count them as you store them in the mp3s array (you'll need to do this, anyway, to know where to store the next entry in the array).

Then, in findFiles, randomly choose one of the songs to play, and play it. Return only when the song is finished playing.