Go Down

Topic: 4x3 led grid array function? (Read 999 times) previous topic - next topic

dtokez

Dec 09, 2012, 06:14 am Last Edit: Dec 09, 2012, 06:18 am by dtokez Reason: 1
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

guix

#1
Dec 09, 2012, 06:50 am Last Edit: Dec 09, 2012, 07:03 am by guix Reason: 1
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:

Code: [Select]

// 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)

dtokez

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

dtokez

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.
Code: [Select]
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

Code: [Select]
#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);

}




lloyddean

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

Code: [Select]

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

Go Up