three random blinks

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jim
 
Posts: 106
Joined: Wed Feb 20, 2008 12:15 am

three random blinks

Post by jim »

hi all,
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
}
I changed "Blink without delay" to blink three LEDs at three different (but regular) rates.

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);
    
  }
}
Now i am failing to join these two. Any hints or clues would be appreciated.
thanks

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: three random blinks

Post by adafruit_support_bill »

Each time through your loop:
  • Generate a random number between 1 and 3. This selects a LED state to change.
    Change the state of the selected LED.
    Generate a random number for your delay time.
    Delay.

User avatar
jim
 
Posts: 106
Joined: Wed Feb 20, 2008 12:15 am

Re: three random blinks

Post by jim »

thanks

User avatar
jim
 
Posts: 106
Joined: Wed Feb 20, 2008 12:15 am

Re: three random blinks

Post by jim »

now i have to figure out how to keep my lights off instead of on most of the time.
your comments helped a bunch,
thanks

Code: Select all

long randPin;
long randTime;

int ledPin1 = 9;                // LED connected to digital pin 9
int ledPin2 = 10;                // LED connected to digital pin 10
int ledPin3 = 11;                // LED connected to digital pin 11



void setup(){
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
  
  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() {
  // print a random number from 0 to 49
  randTime = random(50);
  Serial.println(randTime);  
  
  // print a random number from 1 to 3
  randPin = random(1, 4);
  Serial.println(randPin);
  
  

  delay(randTime);
  
  
{
  
  
  // do something here
  
  // if the LED is off turn it on and vice-versa.
    if (randPin == 1)
      ledPin1 == HIGH;
      
    else
      ledPin1 == LOW;
      randPin = random(1, 4);
      randTime = random(50);
      digitalWrite(ledPin1, randTime);
    //delay(randTime);
    
     if (randPin == 2)
      ledPin1 == HIGH;
    else
      ledPin1 == LOW;
      randPin = random(1, 4);
      randTime = random(50);
      digitalWrite(ledPin2, randTime);
   // delay(randTime);
    
     if (randPin == 3)
      ledPin1 == HIGH;
    else
      ledPin1 == LOW;
      randPin = random(1, 4);
      randTime = random(50);
      digitalWrite(ledPin3, randTime);
  //  delay(randTime);
}

}

ron_rjt
 
Posts: 1
Joined: Tue Jul 15, 2014 5:54 pm

Re: three random blinks

Post by ron_rjt »

Jim, I googled random delay for arduino and your post popped up and helped me a lot ..

just wanted to say
thanks heaps. :)

ron.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”