Ideas for Randomly Blinking LEDs?

Hi guys - as part of a larger project, I want to randomly blink each of four LEDs ON and OFF separately, together, and all at once at random times over a period of about 20 seconds. The trick is, each time each LED is on, it can only be on for 100ms. Also, there must be a minimum of 2 seconds between each LED blinking on again.

The "easy" way I have figured out how to do this is with a function that calls each LED HIGH / LOW with delays. Basically, I pre-preogrammed the sequence to meet my needs, which isn't random and uses delays, so I want to see if anyone has a better idea:

void SEQUENCE ()
{
 
  digitalWrite (LED1, HIGH);
  delay (100);
  digitalWrite (LED1, LOW);
  delay (800);
  digitalWrite (LED2, HIGH);
  delay (100);
  digitalWrite (LED2, LOW);
  delay (300);
  digitalWrite (LED4, HIGH);
  digitalWrite (LED3, HIGH);
  delay (100);
  digitalWrite (LED4, LOW);
  digitalWrite (LED3, LOW);
  delay (1500);
  digitalWrite (LED1, HIGH);
  delay (100);
  digitalWrite (LED1, LOW);
  delay (1200);
  digitalWrite (LED2, HIGH);
  delay (100);
  digitalWrite (LED2, LOW);
  delay (950);
  digitalWrite (LED3, HIGH);
  digitalWrite (LED2, HIGH);
  delay (100);
  digitalWrite (LED3, LOW);
  digitalWrite (LED2, LOW);
  delay (250);
  digitalWrite (LED4, HIGH);
  digitalWrite (LED1, HIGH);
  delay (100);
  digitalWrite (LED4, LOW);
  digitalWrite (LED1, LOW);
  delay (250);
  digitalWrite (LED4, HIGH);
  digitalWrite (LED3, HIGH);
  delay (100);
  digitalWrite (LED4, LOW);
  digitalWrite (LED3, LOW);
  delay (2500);
  digitalWrite (LED1, HIGH);
  digitalWrite (LED2, HIGH);
  delay (100);
  digitalWrite (LED1, LOW);
  digitalWrite (LED2, LOW);
  delay (250);
}

One idea I thought about exploring was use of this random number generation strategy:

 int WhichLEDon = random(5);

That will assign a variable I could call "WhichLEDon" a number between 0 and 4 that could correspond to an LED pin #.

How would I go about writing a program that would, over 20 seconds, randomly select an LED, light it up for 100ms, then pick another one, light it up for 100ms, then pick two, light both of them up for 100ms at once, and so on and so forth, while keeping track of when each LED was lit so as to not re-light the same LED in 2 seconds?

Ummm, if you only have 4 LEDs, and you only turn on each light for 100ms, you will only have 400ms or so before you need to repeat a light.

The ISO C standard (which C++ mostly includes) has two functions:

  • void srand (unsigned int) -- set the random number seed
  • int rand (void) -- return a random integer

that could be used.

So you would wait for a button to be pressed, and then feed the millis function as the argument to srand, and then use the result of repeated calls to rand and check the bottom for bits.

Something like this (note I'm just writing this free hand). I'll leave the check for the light being on previously for you to do:

extern void srand (unsigned int);
extern int rand (void);

const int button = 2;
const int led_pins[4] = { 13, 12, 11, 10 };

void
setup (void)
{
  unsigned i;

  for (i = 0; i < sizeof (led_pins) / sizeof (led_pins[0]); i++)
    pinMode (led_pins[i], OUTPUT);

  pinMode (button, INPUT);
  while (digitalRead (button) != HIGH)
    {
      // wait for a button press
      delay (1);
    }

  // Set the random seed based on how long it took the user to
  // press the button.
  srand ((unsigned int) millis ());
}

void
loop (void)
{
  int r = rand ();
  unsigned i;

  // Set each LED on/off based on whether the bit is set
  for (i = 0; i < sizeof (led_pins) / sizeof (led_pins[0]); i++)
    {
       pinMode (led_pin[i], (r & (1 << i)) != 0));
    }

  // Leave the lights on for 100ms
  delay (100);
}