# Control multiple LEDs by defining the flash sequence of each LED vs. time

Hi, Based on the arduino shiftout tutorial I would like to control 48 LED´s with shift registers. The shift register with 8 outputs needs during each loop the information which LED´s were switched on simultaneously (e. g. 11001100). How can I calculate in the sketch itself which led´s were switched on simultaneously if I have the following information as input (the sequence of each led has to be repeated infinitely led1: on 100 ms --> off 400 ms led 1: on 1000 ms --> of 4000 ms led 3: on 100 ms --> off 300 ms --> on 100 ms --> off 300 ms --> on 100 ms --> off 5000 ms led … : .....

I have calculated this with excel and then copied the array into the code. But to be more flexible I want to code the sequence of each led vs. time individually in the sketch. The transformation of this input to the information that the shift registers requires has to be done by the sketch itself.

Thanks in advance! exige

For each LED I would have structure containing:
A pointer to and array of ON and OFF intervals. unsigned int if your intervals are < 50,000
The length of the array of intervals.
A variable to contain the time of the last transition.
A variable with the current interval’s index, starting at 0.

Make an array of these structures.

In loop() go through the list looking to see if currentTime - time of last transition is > the current interval. If so, add the current interval to the last transition time, toggle the bit for the current LED, add 1 to the interval index, and if that is >= the array length, set the index back to 0 and make sure the LED is ON.

Note: all your example intervals start with ON. If that is not always the case you will need a variable for the initial state of each cycle.

Please don't crosspost. You asked the same question in the Programming forum. If Merge worked correctly, I would Merge these two.

If you look in my code library repository (link below) there is a sketch called MultiBlink that implements just what you want except that it works on direct I/O (ie, not a shift register).

Modification should be easy for your purposes, as most of the control logic for pattern flashing is already there.

Thanks for your answers. At the weekend I try to solve this task and post my code here.

p. s. It sounds stupid but the crosspost wasn´t on purpose. I couldn´t find my post in the forum and by using the link "Updated Topics" I got an error message. Therefore I thought that I didn´t post the topic but just clicked on preview. :blush:

exige

I downloaded Multi_Blink from http://arduinocode.codeplex.com/ and the code is working perfectly with the outputs of my arduino.
I tried to modify the code to run LEDs connected to a shift register but this doesn´t work. With the follwing code the first LED is working according the the input table.

``````void MultiBlink(ledTable *pT, uint8_t tableSize)
// Finite state machine that uses the data passed to it to run the show
{
for (int i=0; i < tableSize; i++, pT++)
{
uint8_t  cs = pT->currentState;  // current state shortcut

// check if the state active time has expired (ie, it is less than current time)
if (millis() >= pT->nextWakeup)
{
pT->nextWakeup = millis() + pT->state[cs].activeTime;

switch (pT->state[cs].stateID)
{
case MB_OFF:
case MB_ON:    // Write digital value
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, pT->state[cs].stateID == MB_ON ? HIGH : LOW);
digitalWrite(latchPin, HIGH);
pT->currentState = (pT->currentState + 1) % MAX_STATE;
}
break;

case (...):
{ ...}

}
}
}
return;
}
``````

But I don´t know how to forward the information to the other LEDs. The idea is to merge the state of each pT->ledPin to a byte by using the code from this topic http://forum.arduino.cc/index.php?topic=43174.0 and forward this byte the the shift register.