I started with the below and tested on an Uno using serial monitor. It uses your original version of effect_rain except it does not call *setvoxel* and *shift* but prints to serial monitor; some additional debug was added. It also uses delay instead of delay_ms; reason is that delay_ms did not delay 1 second.

The *for (;;);* is used to only run once (I could have moved it to setup from that perspective).

`void setup()`

{

Serial.begin(57600);

}

void loop()

{

effect_rain(15);

for (;;);

}

void effect_rain (int iterations) {

PORTC = B11001100; // 4

int i, ii;

int rnd_x;

int rnd_y;

int rnd_num;

for (ii = 0; ii < iterations; ii++)

{

Serial.println();

Serial.print("currentTime = "); Serial.println(millis());

Serial.print("iteration = "); Serial.println(ii);

rnd_num = rand() % 4;

Serial.print("rnd_num = "); Serial.println(rnd_num);

for (i = 0; i < rnd_num; i++)

{

rnd_x = rand() % 8;

rnd_y = rand() % 8;

Serial.print("rnd_x = "); Serial.println(rnd_x);

Serial.print("rnd_y = "); Serial.println(rnd_y);

Serial.println("setvoxel(rnd_x, rnd_y, 7)");

}

delay(1000);

Serial.println("shift(AXIS_Z, -1");

}

}

The output

`currentTime = 0`

iteration = 0

rnd_num = 3

rnd_x = 1

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 2

rnd_y = 2

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 0

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1 <<<<<<< little bug in the code here, missing ')'

currentTime = 1021

iteration = 1

rnd_num = 2

rnd_x = 3

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 2039

iteration = 2

rnd_num = 0

shift(AXIS_Z, -1

currentTime = 3040

iteration = 3

rnd_num = 2

rnd_x = 3

rnd_y = 7

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 7

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 4058

iteration = 4

rnd_num = 0

shift(AXIS_Z, -1

currentTime = 5058

iteration = 5

rnd_num = 0

shift(AXIS_Z, -1

currentTime = 6060

iteration = 6

rnd_num = 3

rnd_x = 1

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 5

rnd_y = 0

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 5

rnd_y = 3

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 7086

iteration = 7

rnd_num = 2

rnd_x = 0

rnd_y = 7

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 1

rnd_y = 2

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 8102

iteration = 8

rnd_num = 0

shift(AXIS_Z, -1

currentTime = 9104

iteration = 9

rnd_num = 3

rnd_x = 2

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 3

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 3

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 10130

iteration = 10

rnd_num = 3

rnd_x = 0

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 1

rnd_y = 4

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 11156

iteration = 11

rnd_num = 3

rnd_x = 2

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 1

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 4

rnd_y = 0

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 12182

iteration = 12

rnd_num = 0

shift(AXIS_Z, -1

currentTime = 13184

iteration = 13

rnd_num = 2

rnd_x = 4

rnd_y = 4

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 2

rnd_y = 7

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

currentTime = 14201

iteration = 14

rnd_num = 2

rnd_x = 2

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

When using delay_ms instead of delay, the last iteration started at 922 ms.

`currentTime = 922 <<<<<<< about one second`

iteration = 14

rnd_num = 2

rnd_x = 2

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1

Next I fixed my version of effect_rain. To be able to compare, again serial prints were used.

`void setup()`

{

Serial.begin(57600);

}

unsigned long currentTime;

void loop()

{

currentTime = millis();

if (effect_rain(15) == true)

{

for (;;);

}

}

/*

effect 4 rain

In:

number of iterations

Returns:

true if all iterations completed, else false

*/

bool effect_rain (int iterations)

{

// count the number of iterations; used to be ii

static int count;

// last time that the cube was updated

static unsigned long lastUpdateTime;

PORTC = B11001100; // 4

int i;

int rnd_x;

int rnd_y;

// this now needs to be static so it's remembered between successive calls to effect_rain

static int rnd_num;

// if number of iterations completed

if (count >= iterations)

{

// done, reset count

count = 0;

// and indicate to called that we're finished

return true;

}

// if 'first' call or 1 second lapsed

if (count == 0 || (currentTime - lastUpdateTime >= 1000))

{

Serial.println();

Serial.print("currentTime = "); Serial.println(currentTime);

Serial.print("iteration = "); Serial.println(count);

rnd_num = rand() % 4;

Serial.print("rnd_num = "); Serial.println(rnd_num);

for (i = 0; i < rnd_num; i++)

{

rnd_x = rand() % 8;

rnd_y = rand() % 8;

Serial.print("rnd_x = "); Serial.println(rnd_x);

Serial.print("rnd_y = "); Serial.println(rnd_y);

Serial.println("setvoxel(rnd_x, rnd_y, 7)");

}

count++;

lastUpdateTime = currentTime;

Serial.println("shift(AXIS_Z, -1)");

}

// indicate that effect_rain is still in progress

return false;

}

And the output

`currentTime = 0`

iteration = 0

rnd_num = 3

rnd_x = 1

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 2

rnd_y = 2

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 0

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 1000

iteration = 1

rnd_num = 2

rnd_x = 3

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 2000

iteration = 2

rnd_num = 0

shift(AXIS_Z, -1)

currentTime = 3000

iteration = 3

rnd_num = 2

rnd_x = 3

rnd_y = 7

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 7

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 4000

iteration = 4

rnd_num = 0

shift(AXIS_Z, -1)

currentTime = 5000

iteration = 5

rnd_num = 0

shift(AXIS_Z, -1)

currentTime = 6000

iteration = 6

rnd_num = 3

rnd_x = 1

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 5

rnd_y = 0

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 5

rnd_y = 3

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 7000

iteration = 7

rnd_num = 2

rnd_x = 0

rnd_y = 7

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 1

rnd_y = 2

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 8000

iteration = 8

rnd_num = 0

shift(AXIS_Z, -1)

currentTime = 9000

iteration = 9

rnd_num = 3

rnd_x = 2

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 3

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 3

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 10000

iteration = 10

rnd_num = 3

rnd_x = 0

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 1

rnd_y = 4

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 11000

iteration = 11

rnd_num = 3

rnd_x = 2

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 1

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 4

rnd_y = 0

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 12000

iteration = 12

rnd_num = 0

shift(AXIS_Z, -1)

currentTime = 13000

iteration = 13

rnd_num = 2

rnd_x = 4

rnd_y = 4

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 2

rnd_y = 7

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

currentTime = 14000

iteration = 14

rnd_num = 2

rnd_x = 2

rnd_y = 5

setvoxel(rnd_x, rnd_y, 7)

rnd_x = 0

rnd_y = 1

setvoxel(rnd_x, rnd_y, 7)

shift(AXIS_Z, -1)

With exception of the currentTime values (and the little bug with ')', my modified effect_rain gives the same output results so I'm comfortable that the next post will work for you.

Note the use of the return value of effect_rain in loop. effect_rain returns true if the number of iterations has been completed, else it returns false. In the example, it's used to only run a full number of iterations once.