Go Down

Topic: Random flash using millis (Read 374 times) previous topic - next topic

STDummy

Hi Guys

I just know somehow that this question will bring down scorn and derision, but after a weekend of trying, I just can't seem to get my head around being able to independently control the on and off periods of a flashing led if they need to be random.  I simply want to have a led flashing at random on and random off intervals but for the process not to affect the timing of several other leds that are flashing at regular intervals.

Here is my code so far:

Code: [Select]
int RandPin = 13;
int NavPin1 = 12;
int NavPin2 = 11;
int StrobePin1 = 10;
int StrobePin2 = 9;

int ledState = LOW;
long previousMillis = 0;
long interval = (50);

void setup() {
  pinMode (RandPin, OUTPUT);
  pinMode (NavPin1, OUTPUT);
  pinMode (NavPin2, OUTPUT);  // Duplicate of NavPin1
  pinMode (StrobePin1, OUTPUT);
  pinMode (StrobePin2, OUTPUT);   // Duplicate of StrobePin1

}

void loop() {
 
// Fade navPins up then down
  for (int i = 0 ; i<= 255; i += 5){
    analogWrite (NavPin1, i);
    analogWrite (NavPin2, i);
    delay (2);
  }
  for (int i = 255 ; i>= 0; i -= 5){
    analogWrite (NavPin1, i);
    analogWrite (NavPin2, i);
    delay (2);
  }

// Double Flash StrobePins
  delay (850);
  digitalWrite (StrobePin1, HIGH);
  digitalWrite (StrobePin2, HIGH);
  delay (50);
  digitalWrite (StrobePin1, LOW);
  digitalWrite (StrobePin2, LOW);
  delay (100);
  digitalWrite (StrobePin1, HIGH);
  digitalWrite (StrobePin2, HIGH);
  delay (50);
  digitalWrite (StrobePin1, LOW);
  digitalWrite (StrobePin2, LOW);
  delay (850);

//Flash RandPin

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

// ** I need to change this to produce random interval flashing **

    // 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(RandPin, ledState);
  }
}


The RandPin flashing part of the code is taken verbatim from the Blink without Delay sketch and of course works ok as it is, however I would like to try and make the on and off intervals vary randomly for each flash of the led.  What is messing with my head is trying to understand where the random values need to be updated for each flash.  Can anyone give me any pointers please?

PaulS

Quote
I just can't seem to get my head around being able to independently control the on and off periods of a flashing led if they need to be random.

What needs to be random? The pin number? The on time? The off time? Any one or all can be.

If you are trying to have several LEDs flash at different intervals, having one last event time is going to be a problem. You need one for each LED.

Using millis() and delay() in the same code hardly makes sense.

STDummy

Hi PaulS

The random element needs to be the on time and the off time for each cycle of the LED attached to the pin designated "RandPin".  I'm trying to simply random flicker the led while the other leds are regularly flashed

You're probably absolutely right about the event timing. Its just that the part of the sketch using delay() was a sketch I already have been using for some time and I was trying to extend it with the extra random flicker function.  I've got fixated on the problems associated with where to randomise the on and off time values for RandPin and hadn't got to the stage where I would use millis() on the sketch as a whole.

PaulS

Quote
I've got fixated on the problems associated with where to randomise the on and off time values for RandPin and hadn't got to the stage where I would use millis() on the sketch as a whole.

You need to start with a new sketch that does nothing but turn some LEDs on and off at random intervals. Once that works, you'll notice that stuff happens on each pass through loop.

You should then be able to see how to turn on and off LEDs at regular intervals. The only difference between random intervals and fixed intervals is that the fixed intervals don't change.

Henry_Best

Try this (untested):
Code: [Select]

float interval = 500;
...
if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    interval *= (random(1, 50)/25);   //changes interval by a random
        //amount each time the LED is switched on or off.

STDummy

Thanks Guys  :)

As soon as I can (I'm currently at work), I'll try out Henry's suggestion in a sketch on its own, as suggested by PaulS and see how I get on from there.

Once I've mastered that, its on to working out how to make the on interval different by a random amount to the off interval at each pass!

I feel a headache coming on......!

Go Up