I'm working on a project that will be storing several audio messages in a few different "groups", to identify each as unique sets of objects from the other sets. Currently, I'm thinking of using arrays to store the data. Once the data is stored, I want to be able to delete values and then shift the other values in the array to the "front" so that I will be able to write to the first "empty" (value of 0) block in the array. I also want to be able to access a random (assuming pseudo-random) filled sector in the array. For now, I'm starting with ints just to get the theory down. As far as shifting the values goes, I've come up with the following code, but I want to see if there is some more efficient way to go about it.
const int capacity = 5;
int testArray[capacity] = {1,2,0,3,4};
void arrayShift()
{
for(int i=0; i<4; i++)
{
if (testArray[i] == 0)
{
testArray[i] = testArray[i+1];
testArray[i+1] = 0;
}
}
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print(testArray[0]);
Serial.print(" ");
Serial.print(testArray[1]);
Serial.print(" ");
Serial.print(testArray[2]);
Serial.print(" ");
Serial.print(testArray[3]);
Serial.print(" ");
Serial.print(testArray[4]);
Serial.print("\n");
arrayShift();
Serial.print(testArray[0]);
Serial.print(" ");
Serial.print(testArray[1]);
Serial.print(" ");
Serial.print(testArray[2]);
Serial.print(" ");
Serial.print(testArray[3]);
Serial.print(" ");
Serial.print(testArray[4]);
Serial.print("\n");
Serial.end();
}
After that, I want to be able to access a random "filled" section of the array, but I'm really not at all sure how to do that. I've read about using time to create a psuedo-random value using mathematical formulae, such as using modulo to divide millis in order to generate a value, and things along those lines, but I'd like to get some of your opinions, too. Thanks so much for the help guys! These forums never fail to impress!
EDIT: I didn't look up random number Arduino. Found the random() operator, so now I just need to write a couple if statements and loops to make sure it doesn't pick a number greater than the capacity of the array, or an empty element of the array. My idea is currently to limit the max number using a numberOfElements variable to count the number of filled elements in the array to kill both birds with one stone, but we'll see how it works. For now, I just want to see if you guys have any better ideas about shifting array values. Thanks again!