Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: johnteh1980 on May 10, 2012, 01:09 am

Title: Multiple LED patterns independently
Post by: johnteh1980 on May 10, 2012, 01:09 am
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:

http://www.youtube.com/watch?v=VDtRCbOoTGA

Any pointers in the hardware id need and programming techniques would be great. Im using a single Arduino Nano and have experience in programming.
Title: Re: Multiple LED patterns independently
Post by: johnwasser on May 10, 2012, 05:16 am
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[] = {
  10,11,12,13};
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);
  }
}
Title: Re: Multiple LED patterns independently
Post by: johnteh1980 on May 10, 2012, 12:29 pm
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?
Title: Re: Multiple LED patterns independently
Post by: johnwasser on May 10, 2012, 03:22 pm
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.
Title: Re: Multiple LED patterns independently
Post by: johnteh1980 on May 11, 2012, 01:26 am
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))
  {
    animateEar();
    lastEarFrame = currentTime;
  }
 
  // check to see if its time to animate the cheek 
  if(currentTime > (lastCheekFrame+cheekSpeed))
  {
    animateCheek();
    lastCheekFrame = currentTime;
  }
 
  // check to see if its time to animate the coloured bars 
  if(currentTime > (lastBarFrame+barSpeed))
  {
    animateBars();
    lastBarFrame = currentTime;
  }
 
  // check to see if its time to animate the chin leds 
  if(currentTime > (lastChinFrame+chinSpeed))
  {
    animateChin();
    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.
Title: Re: Multiple LED patterns independently
Post by: ajcatalano on May 21, 2019, 07:39 pm
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.
Title: Re: Multiple LED patterns independently
Post by: Vintagecloak on Jul 16, 2019, 02:33 am
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?