4x3 led grid array function?

Hi all, I want to control 12 lights (3 columns 4 rows) like this:

000
000
000
000

Is it possible to program a few different modes (eg centre two lights on only, outside lights on only) using some kind of array?
for example:

111
101
101
111

I don't want to have to do lots of digital writes for each light per different modes.

Each light will have its own pin so I will be using 12 digital pins. Hope that all makes sense

Thanks

Yes, it's possible with an array, but you could do something like that, instead of an array for each mode, have a single integer:

// the pins where each led is connected, in reverse order
const uint8_t LED_Pins[] = { 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

// set a mode using a single int
const uint16_t MODE_1 = 0b111101101111; // this is your example (middle LEDs OFF) in one line
 
void SetLEDArrayMode( uint16_t mode )
{
  for (uint8_t i = 0; i < 12; i++)
    digitalWrite( LED_Pins[i], bool( mode & (1 << i) ) );
}
 
void setup()
{
    SetLEDArrayMode( MODE_1 );
}

Note, the reverse order is because using this method, the bits are read in reverse order too. But if your modes are always "symmetrical", then the order of the pins array will have no importance.. 8)

Thanks, I will try that out later in some code and see if I can work out how you have done it :slight_smile:

I'm just trying to implement your example into some simple code to control LED's via a TLC5940 driver IC

I'm getting confused though, instead of using digital write I need to send a 12bit value to control the brightness of the LED's.

How could I write an array for the values to be sent i.e. const uint16_t MODE_1 = 4095, 4095, 1000, 0, 0, 500, 700.... and send those values to the 5940?

this is what I have at the moment

#include "Tlc5940.h"

const uint16_t MODE_1 = 0b111101101111; // this is your example (middle LEDs OFF) in one line
const uint8_t LED_Pins[] = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

void setup()
{
  Tlc.init(0);
}


void SetLEDArrayMode( uint16_t mode )
{
  for (uint8_t i = 0; i < 15; i++)
    Tlc.set(LED_Pins[i], bool( mode & (1 << i) ) );
}



void loop()
{
  
  SetLEDArrayMode( MODE_1 );
  
  //int PinNum;
  //for (PinNum = 0; PinNum < 16; PinNum = PinNum + 1)
  //{
  //  Tlc.set(PinNum, 500);
  //}

  //Tlc.update();
  //delay(1000);

  // Tlc.set(0, 4095);
  //Tlc.set(15, 4095);

}

If it helps keep in mind you can visualize the result with this layout for building your patterns and it will still compile.

uint16_t n = 0b0000/   // ignor
             111/
             101/
             101/
             111;

lloyddean:
If it helps keep in mind you can visualize the result with this layout for building your patterns and it will still compile.

uint16_t n = 0b0000/   // ignor

111/
             101/
             101/
             111;

Les, that looks great and very good for visualisation! I still can't work out how to send the 12 bit values to the TLC chip though instead of a 1 or a 0?

I have no way of knowing how you've wired the 16 channels to the LED's so pattern may be reversed

Untested and uncompiled because I'm to lazy to download the driver library to compile against.

#include "Tlc5940.h"

const uint16_t pattern = 0b0000/   // ignore these 3-bits
                         111/
                         101/
                         101/
                         111;


void SetLEDArrayMode(uint16_t pattern, uint16_t luminance)
{
    for ( int mask = 0b0000100000000000, chan = 0; mask; mask >>= 1, chan++ )
    {
        Tlc.set(chan, ((pattern & mask) ? luminance : 0 ));
    }
}

void loop()
{
    // 12-bit luminance, 0 - 4095

    int luminance   = 4095;                 // full brightness
    SetLEDArrayMode(pattern, luminance);
}

void setup()
{
    Tlc.init(0);
}

Hi, thanks for that! I can't get my head around how it would work though (not tried it yet).

What does the mask stuff do?

I always place URL's inline and not as links so that they may be seen before use.

Please see this reference page http://www.cplusplus.com/doc/tutorial/operators/ and read about

'?:' conditional operator
',' comma operator
'&' bitwise operator, specifically 'and'
'>>' bitwise operators, shift-right
'>>=' compound assignment operator

and finally this page http://www.cplusplus.com/doc/tutorial/control/ and the section "The for loop"

lloyddean:
I have no way of knowing how you've wired the 16 channels to the LED's so pattern may be reversed

Untested and uncompiled because I'm to lazy to download the driver library to compile against.

#include "Tlc5940.h"

const uint16_t pattern = 0b0000/   // ignore these 3-bits
                        111/
                        101/
                        101/
                        111;

void SetLEDArrayMode(uint16_t pattern, uint16_t luminance)
{
   for ( int mask = 0b0000100000000000, chan = 0; mask; mask >>= 1, chan++ )
   {
       Tlc.set(chan, ((pattern & mask) ? luminance : 0 ));
   }
}

void loop()
{
   // 12-bit luminance, 0 - 4095

int luminance   = 4095;                 // full brightness
   SetLEDArrayMode(pattern, luminance);
}

void setup()
{
   Tlc.init(0);
}

Hi all. Just working on this idea again but having no success. I can't get the TLC to do anything using either the above code of the code below. How does it work or how should it work? Just want to be able to set a pattern with each bit being a TLC output ie.

111
101
101
111

not sure how that could be implemented into TLC pins?

#include "Tlc5940.h"

const uint16_t pattern = 0b0000/   // ignore these 3-bits
111/
101/
101/
111;


void SetLEDArrayMode(uint16_t pattern, uint16_t luminance)
{
  for ( int mask = 0b0000100000000000, chan = 0; mask; mask >>= 1, chan++ )
  {
    Tlc.set(chan, ((pattern & mask) ? luminance : 0 ));
  }
}

void loop()
{
  // 12-bit luminance, 0 - 4095

  int luminance   = 4095;                 // full brightness
  SetLEDArrayMode(pattern, luminance);
  Tlc.set(pattern, luminance);
  Tlc.update();
  delay(100);
}

void setup()
{
  Tlc.init(0);
}

Try this ...

#include "Tlc5940.h"

const uint16_t pattern = 0b0000/   // ignore these 3-bits
                         111/
                         101/
                         101/
                         111;


void SetLEDArrayMode(uint16_t pattern, uint16_t luminance)
{
    Tlc.clear();

    for ( int mask = 0b0000100000000000, chan = 0; mask; mask >>= 1, chan++ )
    {
        Tlc.set(chan, ((pattern & mask) ? luminance : 0 ));
    }

    Tlc.update();
}

void loop()
{
    // 12-bit luminance, 0 - 4095

    int luminance   = 4095;                 // full brightness
    SetLEDArrayMode(pattern, luminance);
}

void setup()
{
    Tlc.init(0);
}

Hi, thanks I will give it a try tonight many thanks!