Go Down

Topic: Controlling +40 LEDs with different delays and timings  (Read 486 times) previous topic - next topic

kevin331

Hello,
My first thread  :)

I'm working on a project in which there are around 40 to 45 LED s. This project is similar to a dancing LED project with Arduino projects. But What I want to do is that I want to define a unique delay time for each LED.

For a better illustration, assume the code below :

void setup() {
 // initialize digital pin LED_BUILTIN as an output.
 pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
 digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);                       // wait for a second
 digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
 delay(1000);                       // wait for a second
}


when it starts, led gets on , then it waits for 1 second and after that the led gets off. and the cycle repeats. Whatever code you write above   delay(1000);   gets operated and then it encounters the delay.

What if I want to do this with +40 LED s with different timing and delays ? I don't want to apply one delay time for all the LED s  at any moment of running the code. I think I should do the trick using different threads for LED s , as each code for a single led gets operated independently from other LED s . but all the operation should go on parallelly.

What Arduino board is Suitable for this ? Does the processing speed or ability of the boards matter in this case ?

I would appreciate any help and suggestion.



Wawa

Any Arduino can do this, but you might have to use addressable LED strips or port expanders for that many LEDs.
Look at the BlinkWithoutDelay example in the IDE, and try to understand how to manage time with millis() instead of delay().
Start with two or three LEDs.
Leo..

Paul__B

Another consideration - just how are you going to connect these 40 LEDs and what sort of LED are they?

What is the purpose of this project?

Grumpy_Mike

#3
Jun 12, 2019, 08:30 am Last Edit: Jun 12, 2019, 08:36 am by Grumpy_Mike
Quote
I think I should do the trick using different threads for LED s
Threads are something that a Real Time Operating system gives you ( RTOS ). An Arduino does not / should not have one. It just needs good, non blocking, code that is all.

Here is code to blink 8 LEDs independently to get you going.

Code: [Select]

/*
 * Multiple Blink - Mike Cook
 *
 * controls 8 LEDs and blinks each one at an independent rate set by
 * the value in the flashTime array
 */

int pins[] = {18, 19, 22, 23, 25, 26, 27, 28 };  // set up the 8 pins to use as outputs - these are for a mega change according to what setup you have
int flashTime[] = { 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000};  // set up the blink time of each LED
unsigned long int changeTime[] = {0, 0, 0, 0, 0, 0, 0, 0};  // array to hold when next to change the state of each LED

void setup()                    // is run once, when the sketch starts
{
  for(int i=0; i<8; i++){
  pinMode(pins[i], OUTPUT);                    // sets the digital pins as output
  changeTime[i] = millis() + flashTime[i];    // set the time for the next change
  }
}

void loop()                     // run over and over again
{  
  for(int i=0 ; i<8; i++){ // look at each LED timer in turn
    if(millis()>changeTime[i]) {       // it is time to change the state of the LED
        digitalWrite(pins[i], !digitalRead(pins[i]) );  // make the LED the inverse of the current state (it's the ! that inverts)
        changeTime[i] = millis() + flashTime[i];        // set the time to make the next change
    }  // end of time to change the state of the LED if statement
  }  // end of for loop looking at all the LEDs
}  // end of loop() function

Paul__B


kevin331

Any Arduino can do this, but you might have to use addressable LED strips or port expanders for that many LEDs.
Look at the BlinkWithoutDelay example in the IDE, and try to understand how to manage time with millis() instead of delay().
Start with two or three LEDs.
Leo..
Does processing power or flash memory size of Arduino boards matter in this case at all ?
Ok, I'll check out what you suggested. Thanks



kevin331

Another consideration - just how are you going to connect these 40 LEDs and what sort of LED are they?

What is the purpose of this project?

I might need to use SMD LED s  with 1 to 3 watt of power. I guess in this case I should use Relays to turn them on or off. What do you say ? Is it possible ?

I want to use this in my garden to have led dancer light effect all around. But not like the ones you find in market with limited and pre-written lighting effects modes.

kevin331

Threads are something that a Real Time Operating system gives you ( RTOS ). An Arduino does not / should not have one. It just needs good, non blocking, code that is all.

Here is code to blink 8 LEDs independently to get you going.

Code: [Select]

/*
 * Multiple Blink - Mike Cook
 *
 * controls 8 LEDs and blinks each one at an independent rate set by
 * the value in the flashTime array
 */

int pins[] = {18, 19, 22, 23, 25, 26, 27, 28 };  // set up the 8 pins to use as outputs - these are for a mega change according to what setup you have
int flashTime[] = { 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000};  // set up the blink time of each LED
unsigned long int changeTime[] = {0, 0, 0, 0, 0, 0, 0, 0};  // array to hold when next to change the state of each LED

void setup()                    // is run once, when the sketch starts
{
  for(int i=0; i<8; i++){
  pinMode(pins[i], OUTPUT);                    // sets the digital pins as output
  changeTime[i] = millis() + flashTime[i];    // set the time for the next change
  }
}

void loop()                     // run over and over again
{  
  for(int i=0 ; i<8; i++){ // look at each LED timer in turn
    if(millis()>changeTime[i]) {       // it is time to change the state of the LED
        digitalWrite(pins[i], !digitalRead(pins[i]) );  // make the LED the inverse of the current state (it's the ! that inverts)
        changeTime[i] = millis() + flashTime[i];        // set the time to make the next change
    }  // end of time to change the state of the LED if statement
  }  // end of for loop looking at all the LEDs
}  // end of loop() function


Thanks a lot for providing me with a sample code , I'll check it ASAP.

Wawa

Does processing power or flash memory size of Arduino boards matter in this case at all ?
No, not for 40-45 LEDs.
Let's talk again if you want to control more than 500.
Leo..

david_2018

Quote
Does the processing speed or ability of the boards matter in this case ?
Processing power requirements are very minimal, I timed a sketch very similar to the one posted, and it took about 1/2 millisecond to check the timers and update the LEDs using 40 LEDs, so you have plenty of time for some additional code unless you are blinking the LEDs so fast that it isn't visually perceptible.

You will not be able to directly control 40 outputs unless you use a mega, but with shift registers you can use any arduino.

PaulRB

I might need to use SMD LED s  with 1 to 3 watt of power. I guess in this case I should use Relays to turn them on or off.
LEDs in this power range need constant current drivers and large heatsinks. You would need to find constant current driver modules which have control inputs. You can connect these control inputs to Arduino pins or shift register pins.

Do you need to dim the leds, or only switch on/off? If you need dimming then the pins must be capable of PWM, which only a new pins on Arduino can do. So maybe consider modules with PCA9685 chips. These provide 16 PWM outputs each, so you would need 3.

Go Up