MULTIPLEXING WITH A 74HC164

I have in drawers several 74hc164 8-bit shift registers, and I'm planning to use them to have 25 outputs on a standard Arduino. This is for an art installation where a separate control of 25 different lights is needed. I'm in a mess with the code... I've found this piece of code that goes ok making the 25 outputs looping around... but I'm a newbie and I can't reach the stupid task to address each output separately, that is e.g.: 02 on, then delay, then 02 off, delay, 07 on... etc.

Should I use the ShiftOut command? (The 74HC164 is simplest than 595, (is not three-state) it has only data/clock/and reset pins. )

Anyone can help?

int resetPin = 4;
int clockPin = 2;
int dataPin = 3;
int j;
byte data = 0;

void setup() {
byte switch_var = 0x80;
pinMode(resetPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(resetPin,0);
digitalWrite(clockPin,0);
}

void loop() {

digitalWrite(resetPin,1);

for (j=24; j>=0; j--) {

data = 0x01 & (switch_var >> j);

digitalWrite(dataPin,data);
digitalWrite(clockPin,1);
digitalWrite(clockPin,0);

delay (500);

}

}

Sure, something like this:

byte displayArray [ ] = {B00000000, B00000001, B00000010, B00000100, B00001000,
B00010000, B00100000, B01000000, B10000000}; // assumes 1 = on
byte patternArray [ ] = {2,7,5,3,4,6,1,8};  // or whatever you'd like

void setup(){
//declare the shift register pins & stuff is left as an exercise for the reader
}
void loop(){
for (x = 0; x<8; x=x+1){  // make this match your pattern size
shiftout( dataPin, clockPin, MSBFIRST, displayArray [patternArray[x] ] ); // shift out the bit pattern selected
// double array call; for x = 0, pattern[0] = 2, so display[2] = B00000010
// etc for x = 1,2,3,4,5,6,7
// then repeat
delay(500);
shiftout(dataPin, clockPin, MSBFIRST, 0 ); //  clear the display
delay(50);
}
}

As the 164 has no d-type latch on the outputs you get flickering each time you write something to it. This is because the outputs show the intermediate values of the shifting process.

@ Cross Roads: great, I hadn't thougt to the array solution, it's perfect, thanks a lot.

@ Grumpy Mike: yes, now I realized that 595 is much better thanks to the lacth pin... Anyway the flickering is acceptable for this project, as soon as the outputs drive an array of relays, that are luckily rather slow...

Instead of shiftout(), you can use this

SPI.transfer(displayArray [ patternArray [ x ] ] ) ;

will send the data out a lot faster. Make sure you have a decoupling cap on the Vcc pin.

you can reduce the flickering if you reset the registers before sending data.
Also it's worth considering to use 2 bits parallel or more, to reduce flickering.