So, i'm trying to assign random numbers to an array with blank values via a function. The issue is, while it complies and i can upload it onto my board, nothing happens. Keep in mind, i'm barely in my second week of reading up on and trying to understand code so try not to be too harsh. lol
it's not too long, but i'm just to build functional example because it helps me learn. it's ok if an int repeats i'm just trying to build a random array of values.
Thanks for using code tags on your first post. It would be helpful if you could remember to perform Auto Format in the IDE before you post code.
const int totalArray = NUM_LEDS -1;
const int sparkleArray[totalArray];
Two things here. You have made sparkleArray one element shorter than the leds array. Is that what you wanted? Secondly, you made it const, which means you cannot change the values later. Normally, the compiler will complain if you make something a const and then try to assign to it later. But in your case, you haven't done that, because of another error.
int defineSparkleLeds(int totalArray, int sparkleArray[]) {
for(int j = 0; j <= totalArray; j++) {
int sparkleArray[j] = {random(0, totalArray)};
}
return defineSparkleLeds;
}
Quite a few things wrong here. You made your function return an int. But when you call it in loop(), you discard (ignore) the return value. I suspect you should make this function's type void not int. Then you won't need the return statement (which is also wrong in itself, by the way).
Here's the most important error:
int sparkleArray[j] = {random(0, totalArray)};
Putting int at the start of the line creates a new variable, so it does not assign to the array passed in as a parameter. The curly braces are also not required.
Thanks for all the input. I made the changes you suggested (including the auto format ) and took out some variables and replaced them with numeric values and it works great now.