Go Down

Topic: Random wav file selection  (Read 2128 times) previous topic - next topic

scottm31p

Hello.  I am programming the arduino uno to play a wav file from an sd card whenever the accelerometer that is attached detects movement.  I know how to make it play a specific file but i do not know how to tell it to play a file at random on the sd card.  I have 561 wav files to choose from.  Can someone please help me?  The code for the accelerometer works fine.  I can get a specific sound file to play when movement is detected.  I just need how to tell it to play a random file instead of a specific one. 

Kevin77

Won't

int randNumber = random(561);

work?

jurs

Hello.  I am programming the arduino uno to play a wav file from an sd card whenever the accelerometer that is attached detects movement.  I know how to make it play a specific file but i do not know how to tell it to play a file at random on the sd card.  I have 561 wav files to choose from.  Can someone please help me?  The code for the accelerometer works fine.  I can get a specific sound file to play when movement is detected.  I just need how to tell it to play a random file instead of a specific one.  
Number your files from 0 to 560, then select a random number:

Code: [Select]
int fileToplay=random(561);
Count files until you find that file on the SD card and play.

For real random numbers perhaps also use "randomSeed(seed)" to create a different sequence with values from the accelerometer.

scottm31p

I tried the "filetoplay" command in an if statement.  I only numbered the first 100 files.  Here is my code:

#include <Wire.h>               //Include the Wire library
#include <MMA_7455.h>           //Include the MMA_7455 library
#include <SD.h>
#include <SPI.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>

MMA_7455 accel = MMA_7455();    // Make MMA7455 object
char zVal;          // Return value variables
TMRpcm tmrpcm;
void setup() {
  Serial.begin(9600);
  tmrpcm.speakerPin = 9;
  pinMode(10, OUTPUT);
  SD.begin(4);
 
  accel.calibrateOffset(8, 25, -75);
}
void loop() {
       
   zVal = accel.readAxis('z');   // Read Z Axis
  
  if ((zVal >= 0) || (zVal <-3))
   int fileToplay=random(100);
   Serial.println (zVal, DEC);
   delay(2000);

I tried a specific sound file with the code: tmrpcm.play ("50.wav") instead of the file to play code and it worked.  The delay at the end was needed to prevent the file from being triggered back to back.  The file to play as seen above isnt working.  I am new to programming the arduino.  I appreciate the help you all are giving me with this.  Please let me know what I need to do.  Thank you.

UKHeliBob

Quote
he file to play as seen above isnt working.  I am new to programming the arduino
No surprise there as all you are doing is choosing a (sort of) random number.  The number chosen needs to be turned into a string and to have the extension added then the play function needs to be called with the filename.

This should give you an idea how to do it
Code: [Select]
char fileName[20];    //an array to hold the filename
int fileToPlay ;

void setup()
{
    Serial.begin(115200);
    fileToPlay = random(100); //a random number
    sprintf(fileName, "%d.wav", fileToPlay);  //turn the number into a string and add extension
    Serial.println( fileName);
}

void loop()
{
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

scottm31p

Thank you, thank you, thank you!  Totally awesome!  It works beautifully. 

Go Up