blink LED randomly

Hi

I have 5 LEDs connected to PINs 9-13 (Ardruino Uno)
I'd like to blink each of them separately in a way that each of them have different LOW and HIGH time.

Also I'd like to have a number/millisecond range, let's say I want a random number between 200-400 ms for LOW and 300-500 ms for HIGH. Each time the code runs (loops) each LED must have different LOW and HIGH time, than in previous cycle.

Thanks in advance for help.

Since you are doing basically the same thing with five LEDs I would start with arrays to store each data item:

const byte NUM_LEDS = 5;
const byte LedPins[NUM_LEDS] = {9, 10, 11, 12, 13};
bool LedState[NUM_LEDS];  // Defaults to 'false'/LOW
unsigned long LedLastStateChangeTime[NUM_LEDS];
unsigned long LedOnInterval[NUM_LEDS];
unsigned long LedOffInterval[NUM_LEDS];
void setup() {
    for (int i=0; i < NUM_LEDS; i++) {
        pinMode(LedPins[i], OUTPUT);
        pickTimes(i);  // Randomly assign On and Off intervals to this LED
        LedLastStateChangeTime[i] = millis();  // When the initial off interval started
    }
}

In loop() you'd check to see if the current state (On or Off) should end by comparing elapsed time to the interval. When the state ends, switch to the other state and set the LedLastStateChangeTime. If the OFF interval is ending, call pickTimes() to pick two new random intervals. Hint: use the 'random' function.

rigo22:
I have 5 LEDs connected to PINs 9-13 (Ardruino Uno)
I'd like to blink each of them separately in a way that each of them have different LOW and HIGH time.

Also I'd like to have a number/millisecond range, let's say I want a random number between 200-400 ms for LOW and 300-500 ms for HIGH. Each time the code runs (loops) each LED must have different LOW and HIGH time, than in previous cycle.

Here is some code example for that:

// create a user defined data structure with some variables per LED
struct led_t {byte pin; int lowTime; int cycleDuration; unsigned long cycleStartTime;};

led_t leds[]= { // create an array of led_t data structures
   {9},   // we just initialize the pin numbers here
   {10},
   {11},
   {12},
   {13},
};

#define NUMLEDS (sizeof(leds)/sizeof(leds[0]))

void handleLED(byte index)
{ // first check if the LEDs cycleDuration has passed
  if (millis()-leds[index].cycleStartTime>=leds[index].cycleDuration)  
  { // if so, calculate new values for the LEDs cycleStartTime, lowTime, cycleDuration 
    leds[index].cycleStartTime += leds[index].cycleDuration;
    leds[index].lowTime=200+random(201);
    int highTime= 300+random(201);
    leds[index].cycleDuration= leds[index].lowTime + highTime;
  }
  // annd finally set the LED pin to LOW or HIGH accordingly to the time within the cycleDuration
  if (millis()-leds[index].cycleStartTime<leds[index].lowTime)  
    digitalWrite(leds[index].pin,LOW);
  else  
    digitalWrite(leds[index].pin,HIGH);
}

void setup() {
  for (int i=0; i<NUMLEDS; i++) pinMode(leds[i].pin,OUTPUT);
}

void loop() {
  for (int i=0; i<NUMLEDS; i++) handleLED(i);
}

The 'random' numbers generated are 'pseudo random numbers', so while the numbers are different in each cycle, the program will always create the same sequence of random numbers after the program starts after a controller reset.

If you want to create 'true random numbers' instead of the same sequence of random numbers with each restart of the program, you'd have to initialize the random number generator in a true random way.

Read this - https://www.arduino.cc/en/Reference/Random

Thanks guys for such a fast reply. I will try John's script first, but I have error message: "'pickTimes' was not declared in this scope"
I don't know, how to decalre pickTimes. I assume I have to do this before void setup.

Try this little ditty, you can easily extend it to 5.

const byte red = 9, grn = 10, yel = 11; // LED pins
const byte dur = 30; // duration of blink
const int mn = 500, mx = 2000; // interval between blinks
unsigned long strt1, strt2, strt3;
unsigned long ival1, ival2, ival3; // interval between blinks
byte cnt;

void setup() {
  Serial.begin(9600);
  Serial.println (F(__FILE__"\n"));
  pinMode(red,OUTPUT);
  pinMode(grn,OUTPUT);
  pinMode(yel,OUTPUT);
  strt1 = strt2 = strt3 = millis();
}

void loop() {
  if(cnt > 25){
    randomSeed(analogRead(5)); // new seed after 25 red blinks
    cnt = 0;
  }  
  if(millis() - strt1 > ival1) digitalWrite(red,HIGH);
  if(millis() - strt1 > ival1 + dur){
    digitalWrite(red,LOW);
    ival1 = random(mn,mx);
    strt1 = millis(); cnt++;
  }
  if(millis() - strt2 > ival2) digitalWrite(grn,HIGH);
  if(millis() - strt2 > ival2 + dur){
    digitalWrite(grn,LOW);
    ival2 = random(mn,mx);
    strt2 = millis();
  }
  if(millis() - strt3 > ival3) digitalWrite(yel,HIGH);
  if(millis() - strt3 > ival3 + dur){
    digitalWrite(yel,LOW);
    ival3 = random(mn,mx);
    strt3 = millis();
  }

}

I didn't write loop() or pickTimes() for you because then you wouldn't learn anything and I'd have to charge you $100 for custom programming ($20/hour, 5 hour minimum). :slight_smile:

johnwasser:
I didn't write loop() or pickTimes() for you because then you wouldn't learn anything and I'd have to charge you $100 for custom programming ($20/hour, 5 hour minimum). :slight_smile:

That's fair enough. To learn to continue your script it would take me 3 years :slight_smile: learning excluded
I will use Jur's code, which works fine for what I need.

Thanks again for everybody!

@John: With your skill set, your price is too low!