This is my first 'look-up' sketch and it's working OK. But is there a better way to do it please?
Background: 26 folders contain a total of 2,814 music tracks on a micro-SD card. The DFRobot Mini MP3 Player module cannot report directly on the folder and track that is playing (e.g. when using its randomAll() function). It always delivers an 'indexed' number, which I refer to as 'rawTrack'. My array cumulTracks[] shows the cumulative number of tracks for each folder.
// Convert the indexed track number ('rawTrack') to folder and track within it.
// Various examples
//int rawTrack = 28; // Should give folder = 1, track = 28
//int rawTrack = 97; // Should give folder = 2, track = 1
int rawTrack = 441; // Should give folder = 4, track = 10
//int rawTrack = 2731; // Should give folder = 25, track = 73
//int rawTrack = 2814; // Should give folder = 26, track = 83
int folder;
int track;
int const cumulTracks[26] {96, 202, 431, 590, 778, 920, 1034, 1207, 1244, 1270, 1348, 1600, 1671, 1740, 1925, 1949, 2030, 2192, 2377, 2454, 2545, 2578, 2615, 2658, 2731, 2814};
void setup()
{
Serial.begin(115200);
delay(200);
Serial.println("RawConversion-1");
// Folder 1 range test is special
if (rawTrack <= cumulTracks[1])
{
folder = 1;
track = rawTrack;
}
// Now test other 25 ranges
for (int i = 25; i > 0; i--) // Process folder 26 to folder 2
{
if (rawTrack <= cumulTracks[i] && rawTrack > cumulTracks[i - 1])
{
folder = i + 1;
track = rawTrack - cumulTracks[i - 1];
}
}
Serial.print("folder = "); Serial.println(folder);
Serial.print("track = "); Serial.println(track);
}
void loop()
{
}