Pages: [1]   Go Down
Author Topic: can I map a word to pins?  (Read 546 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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.
Thanks
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 30
Posts: 887
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33314
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is that how its done?
Yes sort of, see:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
and
http://www.thebox.myzen.co.uk/Workshop/LED_Matrix.html
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
« Last Edit: June 17, 2012, 06:01:47 am by a.mlw.walker » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33314
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: