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:
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?