Multiplexing pin addressing help needed

Hello. Sorry if this noob question has been asked and answered before. I'm sure it has but I don't know what search terms to use to try to find the answer here so I'm asking it, and I'm sure to many its VERY basic.

So I have and Arduino Uno driving five tpic6b595's daisychained to light up to 40 leds( for now, operate solenoids later) and I've made good progress building it and being able to turn on any given led, such as led#31 by using shiftOut statements in my sketch like this;

shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
shiftOut(dataPin, clockPin, MSBFIRST, B00000010);
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);

But there must be a better way right? I would like to be able to just do something like
(digitalWrite 31, HIGH) and it should know to turn on the second led on the fourth register without me having to manually pump all this stuff through the other registers to get there?
Thanks for any insight.
JC

But there must be a better way right?

Yep. Write your own function. It will call shiftOut() the appropriate number of times, with the appropriate value(s).

If you put your five bytes in an array:

byte LEDs[5];

Then you can use a little math to set that bit.

  byteNumber = bitNumber / 8;
  bitNumber = bitNumber % 8;  // % is the Modulo operator
 LEDs[byteNumber] |= 0b10000000 >> bitNumber;  // >> is shift-right operator

Then you can shift out the five bytes:

  // Shift out the five bytes
  for (int i=0; i<5; i++)
    shiftOut(dataPin, clockPin, MSBFIRST, LEDs[i]);/code]

Thanks much for the quick replies! I guess I feel like I'm taking a giant step backwards from when I was multiplexing tlc5940's several weeks ago. So with the tlc5940's you just go in to the library config file and edit it to tell it how many chips are daisychained, then in the program you tell it to light pin#31 or whatever and it just knows. Am I just whining? lol... or could we say that the tpic6b595 (or other 595's) have less sophisticated Arduino programming support for some reason? Although I'm also guessing that it is me who needs a more sophisticated understanding of all this! :slight_smile:

Although I'm also guessing that it is me who needs a more sophisticated understanding of all this!

Feel free to write a library for those particular chips. I'm sure that it will be quite similar to the tlc5940 library.

PaulS:
Feel free to write a library for those particular chips. I'm sure that it will be quite similar to the tlc5940 library.

What? Why? Can't someone just do it for me? Wahhh....kidding.

jcmusix:
So with the tlc5940's you just go in to the library config file and edit it to tell it how many chips are daisychained, then in the program you tell it to light pin#31 or whatever and it just knows.

Feel free to add "const unsigned NumberOfRegisters = 5;" and use 'NumberOfRegisters' instead of the constant '5' in the above code.