Go Down

Topic: Multiple LED patterns independently (Read 3286 times) previous topic - next topic


Im new to this and have been reading about Multiplexing and Shift Registers to control multiple LED matrixes.

My question is: If I have two or more sets of LEDs, and I write a pattern for each one: then how do you run these patterns simultaneously?

In an LED matrix i understand that you would light up each LED very fast to get persistence of vision. Would I need to apply that technique to light up these patterns?

I would like to be able to have these patterns run at different speeds, for example one set of LEDs might be a chaser, another one might play a random pattern etc

This is what i want to do:


Any pointers in the hardware id need and programming techniques would be great. Im using a single Arduino Nano and have experience in programming.


You're not the first to ask.  That's why I wrote this sketch I call DaftPunkLights:
Code: [Select]

const int CheekPins[] = {
  3,4,5,6,7,8,9,8,7,6,5,4};  // gives forward and back
const int CHEEK_COUNT = sizeof CheekPins / sizeof (int);
const int CheekIntervals[] =
  300, 300, 300, 300, 300, 300,
  300, 300, 300, 300, 300, 300}
;  // Milliseconds
unsigned long CheekTime = millis();
int CheekStep = 0;

const int ChinPins[] = {
const int CHIN_COUNT = sizeof ChinPins / sizeof (int);
const int ChinIntervals[] = {
  300, 300, 300, 300}
;  // Milliseconds
unsigned long ChinTime = millis();
int ChinStep = 0;

void setup()
  int i;
  /* Cheek LED's */
  for (i=0; i< CHEEK_COUNT; i++)
    pinMode(CheekPins[i], OUTPUT);  // Yes, it's OK to set the pinMode twice on some pins

  /* Chin LED's */
  for (i=0; i< CHIN_COUNT; i++)
    pinMode(ChinPins[i], OUTPUT);

void loop()
  // Do the cheek animation
  if ((millis() - CheekTime) > CheekIntervals[CheekStep])
    CheekTime = millis();
    digitalWrite(CheekPins[CheekStep], LOW);
    CheekStep = (CheekStep+1) % CHEEK_COUNT;
    digitalWrite(CheekPins[CheekStep], HIGH);

  // Do the chin animation
  if ((millis() - ChinTime) > ChinIntervals[ChinStep])
    ChinTime = millis();
    digitalWrite(ChinPins[ChinStep], LOW);
    ChinStep = (ChinStep+1) % CHIN_COUNT;
    digitalWrite(ChinPins[ChinStep], HIGH);
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Ah, well thats saved me a lot of time.

The question still remains about how you go from this to adding additional LED patterns? You've used up all the pins.

Say you have 5 groups of LEDs on different boards, all running different patterns. If i wanted to run these patterns at the same time, would i have to use Shift Registers to light up the 1st steps of each pattern on each board, then the 2nd step, then the 3rd etc?

Seems a bit basic.

Is there not a chip that i can program for each board/group of LEDs, which i can then control from the main Arduino?


You could use some shift registers to get more output pins and run everything from one Arduino or you could use one Arduino for each set of lights.  Which you choose depends a lot on your skill set.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


I found some code from someone else that has programmed the Daft Punk helmet successfully.

Code: [Select]

  // check to see if its time to animate the ear 
  if(currentTime > (lastEarFrame+earSpeed))
    lastEarFrame = currentTime;
  // check to see if its time to animate the cheek 
  if(currentTime > (lastCheekFrame+cheekSpeed))
    lastCheekFrame = currentTime;
  // check to see if its time to animate the coloured bars 
  if(currentTime > (lastBarFrame+barSpeed))
    lastBarFrame = currentTime;
  // check to see if its time to animate the chin leds 
  if(currentTime > (lastChinFrame+chinSpeed))
    lastChinFrame = currentTime;

From this example I can see that you can indeed run each animation from the same sketch by using the currentTime variable along with the time delays for each function.

Just in case anyone else wants to know.

Many thanks.


Thanks @john lasser - do you know if your code will work with 3 animations?
Trying to get that going by paste/rename items but no luck yet.


John Wasser,

I know this post is quite old but I was wondering if you might still be able to assist. I was able to adapt your code to my needs perfectly, however, is it possible to add code to control the Cheekpin LEDs with a push button, without interfering with the Chin LEDs?

Go Up