Go Down

Topic: can I map a word to pins? (Read 928 times) previous topic - next topic


Hi Guys,
So  I want to ahve a large matrix of leds, but in my code I don't want to have to write digitalWrite(pin, HIGH) for every single LED I want on, because I want different combinations f the leds onat any one time, so I was wondering whether there is a better way to do this. I.e cI make like a library or a header file or something that contains all the combinations of pin numbers, each one as a #define or something, and then when I call upon that define, my code sets those pins high? Im not sure exactly how to expalin it because Im not sure whats possible, but lets say I had a 3x3 matrix of leds, and one combo was having 1, 4, 6 and 8 on then could I have a define like:
Code: [Select]

#define combo1 {1,4,7,8}

and then when I want those led's on, I just pass combo1 to a function that loops through setting those pins of the array high?
Code: [Select]

void func_to_turn_on_leds(int[] pins){
for (int i = 0; i< pins.length; i++){
digitalWrite(pins[i], high);

Is that how its done?
I get that you could do it with arrays of the combinations but Im not sure how exactly its done with defines/led arrays.


Bytes (not words) are already mapped to the pins by the underlying AVR-LIBC.  PORTA, PORTB, PORTC, and PORTD are all read writeable and control the pins for their respective ports


Perfect Mike, thanks.
That's exactly the kind of thing I was after...


Jun 17, 2012, 01:00 pm Last Edit: Jun 17, 2012, 01:01 pm by a.mlw.walker Reason: 1
Hi Mike,
so got a little more ambitious and Im wondering what to do when I have more than a "byte"s worth of leds. I in fact have 12, so that would fit into 2 bytes, Using binary I can individually select and single led within the first 8 using
Code: [Select]

void display7(int num){
 int mask = 1;
 for(int i=0; i<7; i++){
   if((mask & sevenCode[num]) == 0) digitalWrite(pins[i], LOW);
   else digitalWrite(pins[i], HIGH);
   mask = mask << 1;
I would rather not have to break the line into 2 rows of the matrix, if I could have each row of the matrix having 15 leds, then the algorithm to decide which ones to turn on will be much much simpler.

where pins[] holds the pin numbers in order, and sevenCode[] holds the binary configurations of led's to turn on.
I attempted to change the values stored in sevenCode to hex values because they go up to 15, and changing the limit of the loop in display7 to 15, however nothing after a single bytes worth of leds seems to work - odd behaviour seems to occur for the leds outside of a byte?


Sorry but you will have to post all the code in the state where it doesn't work but you think it should.


Code: [Select]

void display12(int num){
 for(int i=0; i<12; i++){
    digitalWrite(pins[i], ((1<<i) & twelveCode[num]) );

The 'twelveCode' array should be unsigned int (16 bits) so it can hold the 12 bits.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up