Welcome, %1$s. Please login or register. - Did you miss your activation email?

Login with username, password and session length
 

Author Topic: Multiple Random LED flasher/blinkers  (Read 700 times)

Offline Caladonis

  • Captain
  • ****
  • Posts: 336
Multiple Random LED flasher/blinkers
« on: July 22, 2016, 02:13:49 am »
Did a short video on how to do multiple non-delay based Random LED flashers. I am using this for a lighted cockpit project. Enjoy!

Video -https://www.youtube.com/watch?v=hjEIKcxK9jM

Download Source Code - https://www.dropbox.com/sh/2cxe8zh2sfqmp7z/AACjOJBpfJMNWUV_lBa3rC_da?dl=0

or see below
Random Blinker 1
Code: [Select]
// --------- User Defined Inputs ------
// Values based on UNO   
const byte totalLEDs = 5; // Number of LEDS used -- number <255 so use a byte or uint8_t
const byte ledPin[totalLEDs] = {14,15,16,17,18}; //Pins assigned to those LEDS -- number <255 so use a byte or uint8_t
boolean ledState[totalLEDs] =   {0,0,0,0,0}; // What is the current LED state -- used a byte to store true/false as boolean since it is ON or OFF (0 or not 0)
unsigned int ledOn[totalLEDs] = {0,0,0,0,0}; // used to store ON times, using unsigned int to hold time up to 65535 ms) -- used INT (16 bits to store larger values
unsigned int ledOff[totalLEDs] ={0,0,0,0,0}; // used to store OFF times, using unsigned int to hold time up to 65535 ms) -- used INT (16 bits to store larger values
unsigned long previousMillis[totalLEDs] = {0,0,0,0,0}; // What time was the LED last tumned OFF -- Needs to store largest possible value (4 bytes used)
unsigned long currentMillis;


// ----------- Setup  ------------------
void setup(){
  for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
  {
    pinMode(ledPin[currentLED], OUTPUT); //initilize all used LED pins as outputs
    digitalWrite(ledPin[currentLED], LOW); //Turn LEDs off to start
  }
}



// ----------- Main Loop ---------------
void loop(){
  currentMillis = millis();           // Get the current time
 
  for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
  { 
   
  if (currentMillis - previousMillis[currentLED] > (ledState[currentLED] ? ledOff[currentLED]:ledOn[currentLED])){   // using a conditional operator
    digitalWrite(ledPin[currentLED], ledState[currentLED] = !ledState[currentLED]);    // Flip the state and output it
   
    (ledState[currentLED] == 0) ? ledOn[currentLED]=random(400, 800) : ledOff[currentLED]=random(800, 1400);
     
    previousMillis[currentLED] = currentMillis;  // reset the blinker time
   }

}
}
// -------------------------------------

Random Blinker 2

Code: [Select]
// --------- User Defined Inputs ------
// Values based on UNO 
const byte totalLEDs = 5; // Number of LEDS used -- number <255 so use a byte or uint8_t
const byte ledPin[totalLEDs] = {14,15,16,17,18}; //Pins assigned to those LEDS -- number <255 so use a BYTE or uint8_t (1 byte used)
boolean ledState[totalLEDs] =   {0,0,0,0,0}; // What is the current LED state -- used a BYTE to store true/false as boolean since it is ON or OFF (0 or not 0) (1 byte used)
unsigned int ledOn[totalLEDs] = {0,0,0,0,0}; // used to store ON times, -- used UNSIGNED INT to hold time up to 65535 ms) -- used INT (2 bytes used)
unsigned int ledOff[totalLEDs] ={0,0,0,0,0}; // used to store OFF times
unsigned int ledOnMIN[totalLEDs] = {50,400,800,1000,1200}; // used to store MIN ON times
unsigned int ledOnMAX[totalLEDs] = {100,800,1600,2000,2400}; // used to store MAX ON times
unsigned int ledOffMIN[totalLEDs] = {50,250,450,650,850}; // used to store MIN OFF times
unsigned int ledOffMAX[totalLEDs] = {100,400,650,1000,1200}; // used to store MAX OFF times
unsigned long previousMillis[totalLEDs] = {0,0,0,0,0}; // What time was the LED last tumned OFF -- used UNSIGNED LONG to store largest possible positive value (4 bytes used)
unsigned long currentMillis;


// ----------- Setup  ------------------
void setup(){

  for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
  {
    pinMode(ledPin[currentLED], OUTPUT); //initilize all used LED pins as outputs
    digitalWrite(ledPin[currentLED], LOW); //Turn LEDs off to start
  }
}



// ----------- Main Loop ---------------
void loop(){
  currentMillis = millis();           // Get the current time
 
  for (byte currentLED=0; currentLED<totalLEDs; ++currentLED)
  { 
   
  if (currentMillis - previousMillis[currentLED] > (ledState[currentLED] ? ledOff[currentLED]:ledOn[currentLED])){   // using a conditional operator
    digitalWrite(ledPin[currentLED], ledState[currentLED] = !ledState[currentLED]);    // Flip the state and output it
   
    (ledState[currentLED] == 0) ? ledOn[currentLED]=random(ledOnMIN[currentLED], ledOnMAX[currentLED]) : ledOff[currentLED]=random(ledOffMIN[currentLED], ledOffMAX[currentLED]);

    previousMillis[currentLED] = currentMillis;  // reset the blinker time
   }

}
}
// -------------------------------------

// -- (ledState[currentLED] == 0) ? ledOn[currentLED]=random(ledOn[currentLED], ledOnMAX[currentLED]) : ledOnBase[currentLED]=random(ledOnBase[currentLED], ledOnMAX[currentLED]);

« Last Edit: September 26, 2016, 03:40:05 pm by Caladonis »

Offline texastony1946

  • Ensign
  • *
  • Posts: 20
Re: Multiple Random LED flasher/blinkers
« Reply #1 on: August 05, 2016, 04:20:38 pm »
Thanks for the code. I can use this in an upcoming project.

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1329
    • MW ModelWerks
Re: Multiple Random LED flasher/blinkers
« Reply #2 on: September 23, 2016, 07:01:18 pm »
Very nice.  I like the option to control the rate of random blink on each one.
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline Caladonis

  • Captain
  • ****
  • Posts: 336
Re: Multiple Random LED flasher/blinkers
« Reply #3 on: September 26, 2016, 03:42:29 pm »
I noticed the code for the second one in the post was incorrect, but the drop box code was right so I updated it here -- oops.

 




Enterprise © Bloc