GY-63_MS5611/libraries/Set/examples/randomFromSet/randomFromSet.ino
rob tillaart 51a3493a59 0.2.5 Set
2021-12-28 10:38:33 +01:00

74 lines
1.1 KiB
C++

//
// FILE: randomFromSet.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo random playlist with set
// DATE: 2021-05-04
// URL: https://github.com/RobTillaart/SET
// https://github.com/RobTillaart/Prandom/issues/3
#include "set.h"
Set playList;
int SONGS = 15;
uint32_t start, stop ;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
}
void loop()
{
start = micros();
for (int i = 0; i < SONGS; i++)
{
int s = getRandomSong();
Serial.print(i);
Serial.print('\t');
Serial.println(s);
}
stop = micros();
Serial.print("TIME: ");
Serial.println(stop - start);
delay(1000);
}
int getRandomSong()
{
if (playList.isEmpty())
{
Serial.println("FILL THE ARRAY");
for (int i = 0; i < SONGS; i++)
{
playList.add(i);
}
}
int rv = random(SONGS);
if (playList.has(rv) == false)
{
for (int i = rv; i < rv + SONGS; i++)
{
int idx = i % SONGS;
if (playList.has(idx))
{
rv = idx;
break;
}
}
}
playList.sub(rv);
return rv;
}
// -- END OF FILE --