I am trying to blink three LEDs independently and randomly.
I found "blinking randomly" and changed it to blink three LEDs randomly but only one at a time in sequence.
Code: Select all
/*
* Blink_Randomly
*
* 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.
*
* Original at - http://www.arduino.cc/en/Tutorial/Blink
*/
int ledPin1 = 9; // LED connected to digital pin 13
int ledPin2 = 10; // LED connected to digital pin 13
int ledPin3 = 11; // LED connected to digital pin 13
long randOn1 = 0; // Initialize a variable for the ON time
long randOff1 = 0; // Initialize a variable for the OFF time
long randOn2 = 0; // Initialize a variable for the ON time
long randOff2 = 0; // Initialize a variable for the OFF time
long randOn3 = 0; // Initialize a variable for the ON time
long randOff3 = 0; // Initialize a variable for the OFF time
void setup() // run once, when the sketch starts
{
randomSeed (analogRead (0)); // randomize
pinMode(ledPin1, OUTPUT); // sets the digital pin as output
pinMode(ledPin2, OUTPUT); // sets the digital pin as output
pinMode(ledPin3, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
randOn1 = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff1 = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin1, HIGH); // sets the LED on
delay(randOn1); // waits for a random time while ON
digitalWrite(ledPin1, LOW); // sets the LED off
delay(randOff1); // waits for a random time while OFF
randOn2 = random (200, 1300); // generate ON time between 0.1 and 1.2 seconds
randOff2 = random (400, 500); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(randOn2); // waits for a random time while ON
digitalWrite(ledPin2, LOW); // sets the LED off
delay(randOff2); // waits for a random time while OFF
randOn3 = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff3 = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin3, HIGH); // sets the LED on
delay(randOn3); // waits for a random time while ON
digitalWrite(ledPin3, LOW); // sets the LED off
delay(randOff3); // waits for a random time while OFF
}
Code: Select all
/* Blink without Delay
*
* Turns on and off a 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.
*
* http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
int ledPin1 = 9; // LED connected to digital pin 13
int ledPin2 = 10; // LED connected to digital pin 13
int ledPin3 = 11; // LED connected to digital pin 13
long rand1 = 0; // Initialize a variable for the ON time
long rand2 = 0; // Initialize a variable for the OFF time
long rand3 = 0; // Initialize a variable for the OFF time
int value1 = LOW; // previous value of the LED
int value2 = LOW; // previous value of the LED
int value3 = LOW; // previous value of the LED
long previousMillis1 = 0; // will store last time LED was updated
long previousMillis2 = 0; // will store last time LED was updated
long previousMillis3 = 0; // will store last time LED was updated
long interval1 = 1000; // interval at which to blink (milliseconds)
long interval2 = 900; // interval at which to blink (milliseconds)
long interval3 = 800; // interval at which to blink (milliseconds)
void setup()
{
randomSeed (analogRead (0)); // randomize
pinMode(ledPin1, OUTPUT); // sets the digital pin as output
pinMode(ledPin2, OUTPUT); // sets the digital pin as output
pinMode(ledPin3, OUTPUT); // sets the digital pin as output
}
void loop()
{
rand1 = random (100, 2000); // generate ON time between 0.1 and 1.2 seconds
rand2 = random (300, 4000); // generate OFF time between 0.2 and 0.9 seconds
rand2 = random (500, 6000); // generate OFF time between 0.2 and 0.9 seconds
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
if (millis() - previousMillis1 > interval1) {
previousMillis1 = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value1 == LOW)
value1 = HIGH;
else
value1 = LOW;
digitalWrite(ledPin1, value1);
}
if (millis() - previousMillis2 > interval2) {
previousMillis2 = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value2 == LOW)
value2 = HIGH;
else
value2 = LOW;
digitalWrite(ledPin2, value2);
}
if (millis() - previousMillis3 > interval3) {
previousMillis3 = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value3 == LOW)
value3 = HIGH;
else
value3 = LOW;
digitalWrite(ledPin3, value3);
}
}
thanks