South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« on: December 09, 2012, 12:14:57 am » |
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
|
|
|
|
« Last Edit: December 09, 2012, 12:18:54 am by dtokez »
|
Logged
|
|
|
|
|
France
Offline
God Member
Karma: 19
Posts: 613
Scientia potentia est.
|
 |
« Reply #1 on: December 09, 2012, 12:50:41 am » |
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.. 
|
|
|
|
« Last Edit: December 09, 2012, 01:03:51 am by guix »
|
Logged
|
|
|
|
|
South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« Reply #2 on: December 09, 2012, 11:51:21 am » |
Thanks, I will try that out later in some code and see if I can work out how you have done it 
|
|
|
|
|
Logged
|
|
|
|
|
South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« Reply #3 on: December 27, 2012, 09:47:40 pm » |
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);
}
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #4 on: December 27, 2012, 11:57:43 pm » |
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;
|
|
|
|
|
Logged
|
|
|
|
|
South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« Reply #5 on: December 28, 2012, 09:28:35 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #6 on: December 28, 2012, 12:44:50 pm » |
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); }
|
|
|
|
« Last Edit: December 28, 2012, 07:33:16 pm by lloyddean »
|
Logged
|
|
|
|
|
South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« Reply #7 on: December 29, 2012, 09:42:15 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #8 on: December 29, 2012, 12:20:18 pm » |
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"
|
|
|
|
« Last Edit: December 29, 2012, 12:32:53 pm by lloyddean »
|
Logged
|
|
|
|
|
South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« Reply #9 on: February 07, 2013, 07:53:52 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #10 on: February 07, 2013, 08:06:35 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
South UK
Offline
Sr. Member
Karma: 1
Posts: 479
|
 |
« Reply #11 on: February 08, 2013, 05:00:46 am » |
Hi, thanks I will give it a try tonight many thanks!
|
|
|
|
|
Logged
|
|
|
|
|
|