Play a random file from a directory (WaveShield, WaveHC)

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Play a random file from a directory (WaveShield, WaveHC)

Postby Vincent14 » Fri Feb 15, 2013 6:32 am

Hello, I'm french

I've put some files in a folder on my SD card. I've wrote a function using the folder name, and now my goal is to be able to select a random file then play it on the Wave Shield.

Unfortunatly, I meet a problem with my algorythm:
Code: Select all
dir_t dirBuf;
int folderSize = 0;
while (folder.readDir(dirBuf) > 0)
{
   folderSize ++;
}

folder.rewind();

int randomFile = random(1, folderSize+1);   //random() exclude the max value

int selected = 1;      //There is at least 1 file to play

while (folder.readDir(dirBuf) > 0)
{
   if (selected == randomFile)
   {
      /*
       * Now, translate the dirBuf.name (8.3 format) into
       * a char array comaptible with FatReader open() function
       *
       * Count real characters contained in dirBuf.name:
       * 8.3 mean 8 characters for name and 3 for extension
       * if the file name is under 8 characters, some spaces are added
       */

      int fileNameLength = 1;   //dirBuf.name does not contain the dot
      for (int i = 0; i < 11; i++) //11 because a 8.3 file name has 8+3 characters
      {
         if (dirBuf.name[i] == ' ')
            continue;
         else
            fileNameLength++;
      }
      
      char fileName[fileNameLength];
      int letters = 0;
      for (int i = 0; i < 11; i++)
      {
         if (dirBuf.name[i] == ' ')
            continue;
         if (i == 8)
         {
            fileName[letters] = '.';
            letters++;
         }
         fileName[letters] = dirBuf.name[i];
         letters++;
      }
      
      Serial.println(sizeof(fileName)/sizeof(char));
      Serial.println(fileName);
      
      if (!file.open(folder, fileName))
      {
         error("open the file has failed");
      }
      
      break;
   }
   
   selected++;
}


In the terminal, I get an error very incredible, then the size of the table is smaller than the number of characters that the variable prints! Look at this:

Code: Select all
Searching for Serial monitor (picocom) ... /usr/bin/picocom
Guessing serial port ... /dev/ttyACM0
picocom v1.7

port is        : /dev/ttyACM0
flowcontrol    : none
baudrate is    : 9600
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : yes
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        :
omap is        :
emap is        : crcrlf,delbs,

Terminal ready
8
R2_D.WAVR2_D    WAV
Error: open the file has failed


The file name is however "R2_D.WAV" (8 characters) but I can't understand why "R2_D WAV " is added at the end of my array?

I've tried many things, like putting a println just before and after (to see if a loop could be the reason), or making my own "for()" instead of using println on the array.

I can't understand what happening, could you help me?
Vincent14
 
Posts: 17
Joined: Sun Jan 20, 2013 1:49 pm

Re: Play a random file from a directory (WaveShield, WaveHC)

Postby adafruit_support_bill » Fri Feb 15, 2013 6:50 am

Strings in C require a null character at the end for termination. The extra characters at the end are probably whatever is in memory following your "filename" array.
User avatar
adafruit_support_bill
 
Posts: 15963
Joined: Sat Feb 07, 2009 9:11 am

Re: Play a random file from a directory (WaveShield, WaveHC)

Postby Vincent14 » Fri Feb 15, 2013 8:34 am

I just changed the initialization of "int fileNameLength" to 2 and added "fileName[fileNameLength-1] = NULL;" after the second "for", and it has worked! Thanks very much!

Is there a better way to do that, according to you?
Vincent14
 
Posts: 17
Joined: Sun Jan 20, 2013 1:49 pm

Re: Play a random file from a directory (WaveShield, WaveHC)

Postby adafruit_support_bill » Fri Feb 15, 2013 8:44 am

Is there a better way to do that,

Not really. Your solution is the usual way to do it. :D
User avatar
adafruit_support_bill
 
Posts: 15963
Joined: Sat Feb 07, 2009 9:11 am


Return to Arduino Shields from Adafruit

Who is online

Users browsing this forum: No registered users and 4 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [100]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[108]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[69]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]