Go Down

### Topic: Make 2+ leds blink random (Read 10683 times)previous topic - next topic

#### BigIslandBehr

##### Jan 15, 2011, 08:01 am
I've been using this sketch to make 6-8 leds to blink a preset division of 1000ms:

/* Blink Multiple LEDs without Delay
*
* Turns on and off several light emitting diode(LED) connected to a digital
* pin, without using the delay() function.  This means that other code
* can run at the same time without being interrupted by the LED code.
*/
const int NUMLEDS = 18;
byte pin[NUMLEDS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
byte state[NUMLEDS] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
unsigned long interval[NUMLEDS] = {66, 333, 666, 16, 4666, 233, 16, 33, 1111, 466, 399, 266};
unsigned long time[NUMLEDS];

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

void loop()
{
unsigned long m = millis();

for (int i=0; i<NUMLEDS; ++i)
if (m - time > interval)
{
time = m;
state = state == LOW ? HIGH : LOW;
digitalWrite(pin, state);
}
}

I would like to make the leds blink randomly and the only sketch I've found is the following & for only 1 led:

/*
*
* Modified from the basic Arduino example.  Turns an LED on for a random time,
* then off for a (most likely) different random time.  We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*/

int ledPin = 13;                  // LED connected to digital pin 13
long randOn = 0;                  // Initialize a variable for the ON time
long randOff = 0;                 // Initialize a variable for the OFF time

void setup()                      // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT);        // sets the digital pin as output
}

void loop()                       // run over and over again
{
randOn = random (100, 1200);    // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900);    // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH);   // sets the LED on
delay(randOn);                // waits for a random time while ON
digitalWrite(ledPin, LOW);    // sets the LED off
delay(randOff);               // waits for a random time while OFF
}

Can someone get me started on writing a sketch for 2+ leds to blink random.
Thanks for your patience with a newbie.

#### PaulS

#1
##### Jan 15, 2011, 01:27 pm
Code: [Select]
`I would like to make the leds blink randomly`
You need to define what you want to do a little better. If, on each pass through loop you want to turn a random set of LEDs on, then wait a constant interval, that's one thing.

If you want to turn a constant set of LEDs on, and wait a random interval, that's something else.

Turning each LED on or off for a random amount of time is also possible.

The key is that on each pass through loop, you have to decide what to do with each LED.

Are we to wait for a random period of time to expire, then toggle known LEDs, or are we to choose a random set of LED to turn on, and wait a constant period of time, or are we to determine of a random period of time has expired, then choose a random set of LEDs to trun on and a random interval, or is each LED to have a random interval and random state?

#### BigIslandBehr

#2
##### Jan 15, 2011, 05:34 pm
I'm looking to have each LED go on for a random amount of time, individually, no 2 LED programmed to go on for same amount. I would like each pin 2-13 to be programmed to be random.
Each LED to have a random interval and random state.
Thank you

#### PaulS

#3
##### Jan 15, 2011, 05:56 pm
So, you need an array of times that something was last done, and an array of times to do nothing:

Code: [Select]
`unsigned long lastTime[12];unsigned long idleTime[12];`
In the loop() function, loop through the pins, to see if it is time to do something:

Code: [Select]
`void loop(){   unsigned long currTime = millis();   for(int p =2; p<=13; p++)   {       if(currTime - lastTime[p] > idleTime[p])       {           // Choose whether to turn pin p on or off           // Set pin appropriately           // Choose how long to leave pin p on or off           // Record time to leave pin alone           // idleTime[p] = ???;           // Record now as lastTime[p]           lastTime[p]  =currTime;       }   }}`
You should be able to add code after each of the comments, to choose the state, to set the pin, to choose the on/off duration, etc. Un-comment the idleTime[p] statement and replace the with the appropriate value.