Hey,
i have 9 RGB Led's. Each of this led should light up red, green or blue. Which color the led lights up should be randomly.
Therefore i wrote this method (which works) but i hope that you can give me a better solution:
int rand=random(0,2);
if(rand == 0) {
lightRed(led1);
}
else if (rand == 1) {
lightGreen(led1);
}
else if (rand == 2) {
lightBlue(led1);
}
int rand2=random(0,2);
if(rand2 == 0) {
lightRed(led2);
}
else if (rand2 == 1) {
lightGreen(led2);
}
else if (rand2 == 2) {
lightBlue(led2);
}
.....
int rand9 = random(0,2);
if(rand9 == 0) {
lightRed(led9);
}
else if (rand9 == 1) {
lightGreen(led9);
}
else if (rand9 == 2) {
lightBlue(led9);
}
....way to much code. There must be a more elegant solution.
Thanks for your help.
The original arrangement of lighting up just one color per led is weird, particularly in light of the multiple leds.
Here is what I would do:
#define LED_RAND(led) do {if (random(0, 2)) lightBlue(led); if (random(0, 2)) lightRed(led); if (random(0, 2)) lightGreen(led);} while (0);
//in your application code:
LED_RAND(led1); //randomly lite up a led
LED_RAND(led2); //randomly lite up a led
...
LED_RAND(led9); //randomly lite up a led
It will produce random patterns on each led, up to 8 combinations (including dark/off).