Writing a binary array to shift registers?

So heres my issue. I'm trying to make a library that will light up leds depending on a few things. The only issue is I need to do that to 128 leds. I would be able to get away with 64(16 on max at all times) i guess but the only real way was through shift registers. Basically i have a number (1-128) and i need to translate that to an led. The way i came up with is a huge array:
int leds[128] =
{ 0,0,1,0,0,0,1 ...(and so on) ,0,0 }
where leds[0] is the first led, and leds[128] was the last
in searching for a solution of how to write that i found https://www.arduino.cc/en/Tutorial/ShftOut23

However that only says that i need to write it in hex. Any ideas?

Send the data to two MAX7219, each controlling 64 LEDs.
Each MAX7219 has 8 data registers, each register controls 8 LEDs.

Thus, assuming SPI connections and 2 individual slave select pins:

//1st chip
for (x=0; x<8; x=x+1){
digitalWrite (ss1, LOW);
SPI.transfer (x+1); // address, from 1 to 8
SPI.transter (dataArray1[x]); // dataArray1[0] is LEDs 0 to 7, [1] is 1 to 8, etc., up to 63
digitalWrite (ss1, HIGH);
}
// 2nd chip
for (x=0; x<8; x=x+1){
digitalWrite (ss2, LOW);
SPI.transfer (x+1); // address, from 1 to 8
SPI.transfer (dataArray2[x]);
digitalWrite (ss2, HIGH);
}

I offer breakout board that would make the connections to the LEDs easy
http://www.crossroadsfencing.com/BobuinoRev17/

Hi, its not clear what you want to do. What did you mean by "16 on max at all times"? Later you seem to say that only one led out of 128 will be on, indicating a number 1..128.

What kind of Arduino do you have?

Do you need the leds to fade or just be on/off?

Why do you want to make a library?

If you want only one led on at a time, there is a way to do this without any extra chips using a technique called Charlieplexing. That will need 12 output pins and 12 resistors only.

Paul

PaulRB:
Hi, its not clear what you want to do. What did you mean by "16 on max at all times"? Later you seem to say that only one led out of 128 will be on, indicating a number 1..128.

By this:

I need to do that to 128 leds. I would be able to get away with 64(16 on max at all times)

I assumed he meant. He wants to control 128 leds but could live with using only 64 and only 16 are on at any given point in time.

--- bill

bperrybap:
By this:I assumed he meant. He wants to control 128 leds but could live with using only 64 and only 16 are on at any given point in time.

You could be right Bill. If so, charlieplexing and multiplexing could be combined to allow 16 leds at once, with the addition of 12 transistors and significantly more sketch complexity. Bob's suggestion of 2 x max7219 would probably be easier.

Sorry, i almost forgot about this, Yes i meant That only a max of 16 leds would be on, i have an arduino mega(which is why i need more outputs). I'm sort of confused though how i would get 64 outputs from the 8 on the shift register?

I would love to do that, but i'm not the greatest at binary->hex, could you point me in the right direction?

Instead of

int leds[128] = { 0,0,1,0,0,0,1 ...(and so on) ,0,0 };

you would write

byte leds[16] = { 0b00100010, 0b00100100, 0b11001100 ...(and so on), 0b01101001, 0b11100100 };

Paul

PaulRB:
Instead of

int leds[128] = { 0,0,1,0,0,0,1 ...(and so on) ,0,0 };

you would write

byte leds[16] = { 0b00100010, 0b00100100, 0b11001100 ...(and so on), 0b01101001, 0b11100100 };

Paul

Changing the "pixel" map from one per int to one per bit in multiple bytes changes how the "pixel" data is accessed which requires math to figure out which byte and which bit to test/set vs just a simple index.
While it isn't hard, that too will be different.

Just switching from an int to a uint8_t on the initial array above, while still wasteful, could save half the memory with no other code changes.
--- bill

It sounds to me like the OP wants to send this data out to either shift registers or max7219. However the data is held in memory, it will have to be packed into bytes before that can happen, so some bit manipulation will have to be done, sooner or later.

Updating the array would change from

leds[i] = 1;

to

bitWrite(leds[i>>3], i&7, 1);

You can set/clear single bits of an array:

#define BYTE  unsigned char

BYTE my_bits[] = { 0, 0, 0, 0, 0, 0 };

setBit( my_bits, 37 );       // "37" is bit position (0-47) in array
clearBit( my_bits, 37 );     // ensure bit position <= 47 for a 6-byte array

void setBit( BYTE *A, BYTE POS ) {
    A[POS / 8] |= 1 << (7 - (POS % 8));       // "or" in a bit
}

void clearBit( BYTE *A, BYTE POS ) {
    A[POS / 8] &= ~(1 << (7 - (POS % 8)));    // "and" out a bit
}