random void?

Hello there,

I have a question.
I have, 4 func that's called :

void greenlight(){code}
void bluelight(){code}
void whitelight(){code}
void lcdscreen(){code}

and i call them in the loop like this :

void loop() {
void lcdscreen();
void greenlight();
void bluelight();
void whitelight();
}

the green/blue/white collorcode is fade a led random like a soft star effect. It's working thats ok.
But you see he will first execute lcdscreen, green, than blue and at last white light .
and thats what I dont like 2e green, 3e blue and last white...

I like that the execute of the collor random is
like this : exmp.
lcd, white, green, blue or
lcd, blue white green

But how can I manage that, that random collor void....

Can somebody help me?

Maybe something like this?

int pick( int a, int b) 
{
   return random(2) ? a : b ;
}

void loop() {
  static int next;

  lcdscreen();

  switch (next) {
   case 0: redlight();   next= pick( 1,2);  break;
   case 1: bluelight();  next= pick( 0,2);  break;
   case 2: greenlight(); next= pick( 0,1);  break;
   }

}

if you don't want It to produce the same pseudo-random sequence each time you boot/power it-- see random() - Arduino Reference

Cheers,
John

Oh cool man thx, that works

But hmm can you little explain whats happening in that code, I am a little blocked...

greeting
frederic

and i call them in the loop like this :

void loop() {
void lcdscreen();
void greenlight();
void bluelight();
void whitelight();
}

You don't call them like that, though.

But hmm can you little explain whats happening in that code, I am a little blocked...

John added a new function, to pick a random number, called pick(). Then, in loop, there is a new variable to hold the random number, next.

The switch statement calls a function, based on the value in next, and then assigns a new value to next, by calling the pick() function with some limits on the random value that can be returned.

PaulS:
You don't call them like that, though.

John added a new function, to pick a random number, called pick(). Then, in loop, there is a new variable to hold the random number, next.

The switch statement calls a function, based on the value in next, and then assigns a new value to next, by calling the pick() function with some limits on the random value that can be returned.

Oeps I see it, yea sorry it was lazzy of me, copy past.... and forgot delete that voids..... only
void loop(){
name();
name2();
}

Ha, thx for explaining