Pages: [1]   Go Down
Author Topic: Randomly blink without delay?  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

I've been searching for a way to randomly blink without using delay.
Theres a few codes about to blink without delay, but it seems the only way to randomize this blinking (randomly on/off between a set of values) is with the delay function.

This is the blink without delay code: // http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Code:
// constants won't change. Used here to
// set pin numbers:
const int ledPin =  9;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  // 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, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}


Do you know if this is possible? If so, how?

For the bigger picture - I want a few LED's to randomly blink on/off as a general state, untill someone gets close to them. Then have an LDR sensor, so that when people approach the light, they stop flickering, and stay on dimmed, gradually getting brighter, the closer (less light) someone gets.

I've got the stages in separate codes, but I'm having trouble combining them together.


Cheers
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26618
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but it seems the only way to randomize this blinking (randomly on/off between a set of values) is with the delay function.
Use the blink-without delay tutorial, but instead of the fixed interval ("interval = 1000"), use a value returned (and possibly scaled) by "random".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

oh thanks,

so how would this be written?

sorry - having trouble getting to grips with the arduino language...
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26618
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/Random
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

sweet, that's great!!


THANKSS
Logged

Pages: [1]   Go Up
Jump to: