Arduino motion sensor trigger with Randomizer

Hello all! :slight_smile:

I am working on a project where I am using an ultrasonic sensor and an MP3 shield in order to trigger a sound when motion is detected.

I’ve found code already made to trigger the mp3 to play a file from a micro SD card when movement is detected. What I want to do now is use a randomizer in order to play a random file when movement is detected. All of the files on the micro SD are labeled 001.mp3, 002.mp3 … etc.

So my question is where would I go to learn how to use a random number to grab a random file from the SD.

The code I’m using is as follows:

void loop()
{

Distance=measureDistance(trigPin,echoPin);//measure distance and store
gap=abs(Distance-auxDistance);// calculate the difference between now and last reading

if(firstTime==0){//necesary for stability things
auxDistance=Distance;
gap=0;
//does it only the first time after play a song to avoid first loop malfuntcion
firstTime++;
delay(1000);
}

if(gap>20){ //if distance variation is 20cm
sendCommand(CMD_PLAY_WITHFOLDER, 0X0101);//play the first song of the second folder
firstTime=0;//avoid errors!!we dont like errors
delay(2000);
}

Serial.print(“New Distace:”);//debugggggg
Serial.print(Distance);
Serial.print(" Old Distance: ");
Serial.print(auxDistance);
Serial.println(gap);

delay(300);
auxDistance=Distance;//store the value for the if() in the next loop

// print a random number from 1 to 100
randNumber = random(1, 100);
Serial.println(randNumber);

delay(50);
}

I appreciate any help!

Thank you all!

It looks like you already have some understanding of how to use the random() function and there is plenty of documentation available on the SD library:
https://www.arduino.cc/en/Reference/SD

So what remains is learning how to convert your random number to a filename you can use with the SD library. The SD.open() function takes two arguments: filename and mode. The filename argument should be a string. The return type of random() is long. So you need to convert from long to string. If you can get by with a range of filenames from -32768 to 32767 then the most efficient solution is to use itoa(), which makes it very easy to convert int into a string:
http://www.cplusplus.com/reference/cstdlib/itoa/

int filenameInt = random(0, 100);  // generate a random number
char filenameString[3];  // prepare a buffer to receive the filename. This must be sized to the longest possible length of the string (two digits in this case) + 1 for the string terminator
itoa(filenameInt, filenameString, 10);  // convert the number into its string representation in base 10 and put the string in the buffer

If you do need the full range of long instead of int, you can use ltoa() instead of itoa() it works the same but just uses a little more memory.

As you might have noticed from reading the itoa() documentation, this is a non-standard function. So there is some possibility it wouldn’t be available on certain Arduino boards (the only such I’m aware of are the Intel Galileo and Edison). In that case, you can use sprintf() instead:

int filenameInt = random(0, 100);  // generate a random number
char filenameString[3];  // prepare a buffer to receive the filename. This must be sized to the longest possible length of the string (2 digits in this case) + 4 characters for the extension + 1 for the string terminator
sprintf(filenameString, "%li", filenameInt);  // convert the number into its string representation in base 10 and put the string in the buffer

This should work on all platforms, but it does use a lot more memory than itoa() or ltoa().

So now you have the number converted to the string, but you want to add .mp3 to the end of the string. For this, there is a very useful function called strcat():
http://www.cplusplus.com/reference/cstring/strcat/

I think that’s enough information to get you going. If you get stuck, come back with your updated code from your best attempt.

Please use code tags when you post code or warning/error messages. To do this, click the </> button on the forum toolbar, then paste the text you want to be in the code tags. Finally, move the cursor out of the code tags before adding any additional text you don’t want to be in the code tags. If your browser doesn’t show the posting toolbar, then you can manually add the code tags like this:
[code]``[color=blue]// your code is here[/color]``[/code]

The reason for doing this is that, without code tags, the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier for us to read your code and to copy it to the IDE or editor.

Using code tags and other important information is explained in the “How to use this forum” post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.