Program hangs when using too many random()
I built a Charliecube (4x4x4 LED cube) that cycles through a number of patterns. The original sketch runs through each pattern and colour in sequence for a fixed length of time. To make it more interesting I modified it to randomly vary the colours, the pattern sequence and the length of time each pattern runs.
The problem is, after a random length of time my version of the sketch will not advance to the next pattern. If I remove randomSeed or replace any one of the "random"s with an int (random pattern, random colour or random run time) it will advance after the time set in animationMax.
The changes between the original code and mine are in the sketch (ino). There is one change in cubeplex.h to randomise nextColor. All other header files are unchanged. See the attachment for the full sketch.
Why doesn't it like being random?
Random nextColor from cubeplex.h
/**** Cycle through colours in sequence - removed by chopsuwe ****/
// int nextColor(int color) { return (color+1)%7; }
/**** Cycle through colours randomly - added by chopsuwe ****/
int nextColor(int color) { return (random(0,7)); }
Original Code
#include "cubeplex.h"
int color = red;
void setup() {
initCube();
// how many secconds until the animation is told to progress
animationMax = 10; // this is declared in cubeplex.h
}
void loop() {
planarSpin();
fountian();
trifade();
shiftSquares();
tunnel();
chaseTheDot();
planarFlop3D();
}
/********************************** FOUNTIAN **********************************\
| Light shoots up the middle of the cube then once it reaches the top fall |
| back down on the outside of the cube. After it hits the bottom it changes |
| color and starts again |
| |
| Written By: Asher Glick |
\******************************************************************************/
void fountian() {
continuePattern = true;
int animationSpeed = 200;
while (continuePattern) {
for (int z = 0; z <= 3; z++) {
drawBoxWalls(color,1,1,z,2,2,z);
flushBuffer();
clearBuffer();
delay(animationSpeed);
}
for (int z = 3; z >= 0; z--) {
drawBoxWalls(color,0,0,z,3,3,z);
flushBuffer();
clearBuffer();
delay(animationSpeed);
}
color=nextColor(color);
}
}
My Randomised code
#include "cubeplex.h"
int color = red;
int animationSpeedBase; // multiplied by a constant in each pattern to set the animationSpeed
void setup() {
initCube();
// gnerate a random starting number from analog pin noise.
randomSeed(analogRead(7));
}
void loop() {
// randomly change the speed the animation runs at. Default = 50
animationSpeedBase = random(30,100);
// how many seconds until the animation is told to progress (10-15 seconds)
// moved here from setup() so it will be changed each time through
animationMax = random(10,20); // default = 10
switch(random(0,7)){ // randomly pick a pattern
case 0:
planarSpin();
break;
case 1:
fountian();
break;
case 2:
trifade();
break;
case 3:
shiftSquares();
break;
case 4:
tunnel();
break;
case 5:
chaseTheDot();
break;
case 6:
planarFlop3D();
break;
}
}
/********************************** FOUNTIAN **********************************\
| Light shoots up the middle of the cube then once it reaches the top fall |
| back down on the outside of the cube. After it hits the bottom it changes |
| color and starts again |
| |
| Written By: Asher Glick |
\******************************************************************************/
void fountian() {
continuePattern = true;
int animationSpeed = animationSpeedBase * 4; //random animation speed
while (continuePattern) {
for (int z = 0; z <= 3; z++) {
drawBoxWalls(color,1,1,z,2,2,z);
flushBuffer();
clearBuffer();
delay(animationSpeed);
}
for (int z = 3; z >= 0; z--) {
drawBoxWalls(color,0,0,z,3,3,z);
flushBuffer();
clearBuffer();
delay(animationSpeed);
}
color=nextColor(color);
}
}
charlieCubeAll3.zip (27.5 KB)