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?