Millis random blinking with fixed HIGH time

Hello,

I am making an RC firefighter truck and I found this great code from a forum member RoelDriessen, which suits me great, but I would like to have fixed HIGH time for only two outputs.
Can someone help me with this?

This is the code, I would like to have led5kr and led6kr fixed HIGH output like 20 millis so it would be an effect like a strobo lights with random on feature.

Hope you guys can help me, thank you in advance.

/* Multiple independent blinking LED
 This scetch was build with help of many forum members and contains 3 ways of blinking leds, for example used for Firetrucks in Netherlands, Germany etc
 Led 1+2 are representing normal beacon who have an different interval so prevent a standard left right.
 Led 3+4 are representing blinkers that simultaneously flash 3 times short and wait, this can be used for blinkers in the grill
 Led 4+5 are representing blinkers that flash at a random speed and interval
*/

// Which pins are connected to which LED
const byte led1zw = 2;
const byte led2zw = 3 ;
const byte led3k = 4 ;
const byte led4k = 5;
const byte led5kr = 6 ;
const byte led6kr = 7;

// Time periods of blinks in milliseconds (1000 milliseconds to a second).
// Time variable and constants are always unsigned long
const unsigned long led1zwinterval = 300UL ;
const unsigned long led2zwinterval = 275UL ;
unsigned long previousMillis;
unsigned long previousMillisrandom;
unsigned long previousMillisrandom1;
unsigned long intervalblinkrandom = 200UL;   // not a constant, it will be changed
unsigned long intervalblinkrandom1 = 200UL;   // not a constant, it will be changed
unsigned long currentMillis = millis();
const long periods[] = {50, 50, 50, 50, 50, 50, 1000, 0};
byte index;

// Variable holding the timer value so far. One for each "Timer"
unsigned long led1zwtimer = 0 ;
unsigned long led2zwtimer = 0 ;
unsigned long led3ktimer = 0 ;
unsigned long led4_5ktimer = 0 ;
unsigned long led6krtimer = 0 ;

// Variable to know what the current LED state is
int led1zwState = LOW ;
int led2zwState = LOW ;
int led3kState = LOW ;
int led4kState = LOW ;
int led5krState = LOW ;
int led6krState = LOW ;

void setup() {
 pinMode (led1zw, OUTPUT) ;
 pinMode (led2zw, OUTPUT) ;
 pinMode (led3k, OUTPUT) ;
 pinMode (led4k, OUTPUT) ;
 pinMode (led5kr, OUTPUT) ;
 pinMode (led6kr, OUTPUT) ;
 led1zwtimer = millis () ;
 led2zwtimer = millis () ;
 led3ktimer = millis () ;
 led4_5ktimer = millis () ;
 led6krtimer = millis () ;
}

void loop()
{
 unsigned long currentMillis = millis();
 if ( (millis () - led1zwtimer) >= led1zwinterval ) {
   digitalWrite(led1zw, !digitalRead(led1zw));
   led1zwtimer = millis () ;
 }
 if ( (millis () - led2zwtimer) >= led2zwinterval ) {
   digitalWrite(led2zw, !digitalRead(led2zw));
   led2zwtimer = millis ()  ;
 }
 if (millis() - led4_5ktimer >= periods[index])
 {
   digitalWrite(led3k, !digitalRead(led3k));
   digitalWrite(led4k, !digitalRead(led4k));
   index = ++index % 8;
   led4_5ktimer = millis();
 }
 if ( currentMillis - previousMillisrandom >= intervalblinkrandom)
 {
   previousMillisrandom = currentMillis;
   intervalblinkrandom = random( 25, 300);
   digitalWrite( led5kr, digitalRead( led5kr) == HIGH ? LOW : HIGH);
 }
 if ( currentMillis - previousMillisrandom1 >= intervalblinkrandom1)
 {
   previousMillisrandom1 = currentMillis;
   intervalblinkrandom1 = random( 50, 275);
   digitalWrite( led6kr, digitalRead( led6kr) == HIGH ? LOW : HIGH);
 }
}

You can make it simpler by using only those two outputs and if it works, then add them to the sketch.

I assume that led5 and led6 are independent of each other. They are only short on (20ms), but how long can they be off ?
It helps if you give the numbers, so I don't have to guess.

To do something different for when it is on or off, you have to know if it is on or off. Use either a variable or use a digitalRead(). I prefer a variable. That variable can be the state of the led (HIGH or LOW) or a boolean variable. I choose a boolean variable.

// global variable
bool led5on = false;
const int led5Pin = 6;
unsigned long previousMillisLed5;
unsigned long intervalLed5;

// in the loop()
if ( currentMillis - previousMillisLed5 >= intervalLed5)
{
  if( led5on)
  {
    // led5 is now on, turn it off and set the interval for how long it should be off
    digitalWrite( led5Pin, LOW);
    led5on = false;
    intervalLed5 = random( 150, 500);
  }
  else
  {
    // led5 is now off, turn it on and set a fixed value for the interval
    digitalWrite( led5Pin, HIGH);
    led5on = true;
    intervalLed5 = 20;
  }
  previousMillisLed5 = currentMillis;
}

That sketch is crying out for the use of an array to hold the LED data and to remove the need for repeated code

Take a look at this example sketch and see how the on/off periods for each LED are held in an array. The fourth LED in the example just blinks on/off but as in the sketch the on and off periods do not have to be the same

const byte ledPins[] = {3,5,6,9};
const byte NUMBER_OF_LEDS = sizeof(ledPins);
unsigned long startTimes[NUMBER_OF_LEDS] = {};
byte indexes[NUMBER_OF_LEDS] = {0};
const byte MAX_NUMBER_OF_PERIODS = 10;
unsigned long periods[][MAX_NUMBER_OF_PERIODS] =    //periods for each LED.  zero indicates end of sequence
{
  {1000, 2000, 1500, 2500, 0},  //on/off/on/off/start again
  {500, 200, 1000, 2000, 3000, 4000, 0},  //on/off/on/off/on/off/start again
  {400, 1000, 1500, 2000, 0}, //on/off/on/off/start again
  {1100, 2200, 0} //on/off/start again
};

void setup()
{
  Serial.begin(115200);
  for (int led = 0; led < NUMBER_OF_LEDS; led++)
  {
    pinMode(ledPins[led], OUTPUT);
  }
}

void loop()
{
  unsigned long currentTime = millis();
  for (int led = 0; led < NUMBER_OF_LEDS; led++)  //iterate through the LEDs
  {
    if (currentTime - startTimes[led] >= periods[led][indexes[led]])  //? time to change ?
    {
      digitalWrite(ledPins[led], !digitalRead(ledPins[led]));  //flip led state
      startTimes[led] = currentTime;  //save start time of state
      indexes[led]++;                    //increment index
    }
    if (periods[led][indexes[led]] == 0)  //if next period is zero (end of sequence)
    {
      indexes[led] = 0;        //reset period index for this LED
    }
  }  //end for loop
}

Making the periods data driven allows you to add LEDs and/or change on/off periods without changing the code

Can't say enough how thenkfull I am. I was struggling with this for a week.

THANK YOU both for a really fast respond and special THANK YOU Koepel for solution.

I multiply this into two LED's and setup the correct timing and it works like a charm.

Best regards

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.