I'm wondering if there is a way to randomly select loops in my main loop. For example I have a sketch with various loops in it, each one lights up a LED strip with different patterns and colors. Right now in my main loop, I call these one after the other and they "pllay" in sequence. I'm wondering if there is a way to randomly play the different loops. I was thinking I could assign a number to each loop and then call the random command to play them at random, but can't quite figure out how to do that.
So right now, my main loop looks something like this;
Rather than looping in that order every time, I'm wondering if there is a way to randomly play each of those loops.
Any help is always appreciated.
Use the random() function to generate a random number
Call a different function depending on the random number using if/else
More complicated way
Create an array of function pointers and use the random number as the index to the array and hence which function is called
By the way,
does not call the flashing function
calls the flashing() function assuming that it exists in the program
Here is one way using random and switch case
byte randNum = random(0, 8);
// and so on
Note that when calling functions the parenthesis are necessary.
Keep in mind that changing the pattern only happens when the current pattern is complete. If you decide that you want to send data via the serial port, and have the Arduino change the pattern being played, when it receives a new value, and a pattern that takes 45 minutes to play out has just started, the Arduino won’t know that you want to change patterns for quite a while.
Writing non-blocking code is almost always better than writing blocking code. And, no more difficult.