Multiple random LED's

I have two LED's blink at specified millis while one LED fades in and out. The speed of the fade in and out is controlled with an analog pot.

The code is a mashup of some code from Nick Gammon's site as well as some from the fade sketch in the examples. Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

What I cannot figure out is how to make the two blinking LED's blink randomly in the code instead of at a specified time. I can blink one LED random but I'm not sure how to do it without using code with a random delay time, which would block the other things going on.

As always thanks for any help.

// LED on pins 8 and 13 blink, LED on 9 fade in and out
const byte greenLED = 8;
const byte redLED = 13;
int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
const int analogInPin = A0;
int sensorValue = 0;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 200;
const unsigned long redLEDinterval = 500;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup () 
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
    pinMode(led, OUTPUT);
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis ();  
  }  // end of toggleGreenLED

void toggleRedLED ()
  {
   if (digitalRead (redLED) == LOW)
      digitalWrite (redLED, HIGH);
   else
      digitalWrite (redLED, LOW);

  // remember when we toggled it
  redLEDtimer = millis ();  
  }  // end of toggleRedLED

void loop ()
  {

  // Handling the blink of one LED.
  if ( (millis () - greenLEDtimer) >= greenLEDinterval)
     toggleGreenLED ();

  // The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - redLEDtimer) >= redLEDinterval) 
    toggleRedLED ();
 analogWrite(led, brightness);    
int sensorValue = analogRead(A0);
sensorValue = map(sensorValue, 0, 1023, 0, 30); 
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(sensorValue);                            
/* Other code that needs to execute goes here.
   It will be called many thousand times per second because the above code
   does not wait for the LED blink interval to finish. */

}  // end of loop/code]

Hello,

Maybe something like that?

unsigned long greenLEDinterval;
unsigned long redLEDinterval;

void setup()
{
    greenLEDinterval = random( 200, 500 );
    redLEDinterval = random( 200, 500 );
    ...
}

void loop()
{
    if ( (millis () - greenLEDtimer) >= greenLEDinterval)
    {
        toggleGreenLED ();
        greenLEDinterval = random( 200, 500 );
    }

    if ( (millis () - redLEDtimer) >= redLEDinterval)
    { 
        toggleRedLED ();
        redLEDinterval = random( 200, 500 );
    }
    ...
}

Not sure if that's what you are asking.. :slight_smile:

This is from the project I’m working on. It uses shift registers and shifter library. Basically everything is in arrays. The code is still very much in development so it could use a bit of cleaning up and optimization, but it works.

void neonFlicker(int firstPin, int lastPin)							//  Receives first and last pin number for the block to be assigned neonFlicker effect
{ 
  for (int i = firstPin - 1; i <= lastPin -1 ; i++)       					//  Iterates between the pins set in loop, sets addresses of pins to 1-8 per shift register, and not 0-7
  {
    currentMillis = millis ();                   				//  Updates current time
    if (ledState [i] == 0 && 
      currentMillis - previousMillisArray [i] > neonFlickerArray [i] &&             //  Checks if it is time to flicker
      flickerDelay [i] <= currentMillis)					//  Checks if it is time to turn on one of the outputs on the shift register.
    {
      shifter.setPin(i, HIGH);                                                            	//  If it is time for the pin in question, it is turned on.
      previousMillisArray [i] = currentMillis;                                            	//  Updates corresponding field in the time array.
      ledState [i] = 1;                                                                      	//  Changes the state ledState
      neonFlickerArray [i] = (random (2, 200));                                        //  Sets new flickering delay (time the light is turned on)
    }
    if (ledState [i] == 1 && currentMillis - previousMillisArray [i] > neonFlickerArray [i])    //  Checks if it is time to turn off one of the outputs on the shift register.
    {
      shifter.setPin(i, LOW);                                                             	//  If it is time for the pin in question, it is turned off.
      previousMillisArray [i] = currentMillis;                                               //  Updates corresponding field in the time array.
      ledState [i] = 0;                                                                           //  Changes the state ledState
      neonFlickerArray [i] = (random (2, 4000));                                       //  Sets new flickering delay (time the light is turned off)
    }
  }
  shifter.write();
}

This is my take on lots of LEDs flashing at random times. After a time the random patterns are recalculated.

/* Random blinking lights
for use on a christmas tree
By Mike Cook
*/

#define numberOfLights 16

byte pins[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
byte pinState[numberOfLights];
long int changeTime[numberOfLights];
int flashRate[numberOfLights];
long flashChange;   // how often to change the flashing patterns

void setup() {
  for(int i = 0; i< numberOfLights; i++) {
    pinMode(pins[i], OUTPUT);
    changeTime[i] = millis() + random(1000, 200);
    pinState[i] = LOW;
  }
  setFlashTime();
}

void loop() {
  for(int i = 0; i < numberOfLights; i++) {
   if(changeTime[i] <= millis()) {
   pinState[i] = ~pinState[i];
   digitalWrite(pins[i], pinState[i]);
   changeTime[i] = millis() + flashRate[i];
   } 
  }
  if(flashChange <= millis()) setFlashTime();
}

void setFlashTime(){
  for(int i=0; i<numberOfLights; i++){
    flashRate[i] = random(200, 1500);
  }
  flashChange = millis() + 100000;  // next time to change pattern
}

@Shpaget use the code tags not the quote

Grumpy_Mike:
@Shpaget use the code tags not the quote

OH!!
There was something bothering me about the way it looked, just couldn't put my finger on it. Thanks, fixed.