Go Down

Topic: random player (random at reset) (Read 391 times) previous topic - next topic

bddbddbdd

Mar 30, 2016, 12:04 am Last Edit: Mar 30, 2016, 12:31 am by bddbddbdd
Hi there -
I searched for "random players" and found more than a few examples - but I'm specifically interested in how one would make sure a random player *started* randomly, each time... I had some trouble finding this.

My current code is essentially the same as Michael P. Flaga's... it reads as follows. Can someone help me?

Code: [Select]

// libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

// initialize MP3 card
SdFat sd;
SFEMP3Shield MP3player;
byte result;

// setup
void setup() {
  result = sd.begin(SD_SEL, SPI_FULL_SPEED);
  result = MP3player.begin();
  MP3player.setVolume(12, 12);
  randomSeed (millis());
}

// loop
void loop() {
  MP3player.playTrack(random(5)); // range of tracks to randomly play.
  while (MP3player.isPlaying());   
  MP3player.stopTrack();// stop track
}

uxomm

I do not think that
Code: [Select]
randomSeed(millis());
will produce "random" numbers in your setup.

Take a look at the reference: randomSeed

randomSeed(millis()) can make a good "seed" if there is some interaction with a human pressing a button (not the reset button), but in your setup, millis() will always give the same number and so random will always give the same sequence.

Always decouple electronic circuitry.

borland

You may find a random bias toward selecting certain files.  To remove any random bias, take a look at the Arduino code on from these two blog articles....

http://www.utopiamechanicus.com/article/arduino-better-random-numbers/


http://www.utopiamechanicus.com/article/better-arduino-random-numbers/



 

bddbddbdd

Thanks folks, that does the trick!

Go Up