SN74HC165N Demultiplexing

Hi, I have few of these chips wired in cascade.
I'm also using this code Arduino Playground - ShiftRegSN74HC165N to read buttons status.

It works fine, but the sketch as it is can only handle 4 165's, I would like to add a couple more but I don't have the knowledge to modify the code for that.

Could somebody please give me some indications, or maybe point me another piece of code that can handle a bigger amount of chips?

Thanks in advance

Sure, use SPI.transfer() to read them in.

#include<SPI.h>
byte latchPin = 10; // goes to data latch pin on shift register
byte dataArray[] = {8,}; // increase for as many shift registers are being used
// change counter in for loops to match

byte x; // for loop variable

void setup(){
pinMode (latchPin, OUTPUT);
SPI.begin(); // D13 goes to shift register clock, D12 goes to shift register dataout
SPI.begin(9600);
}

void loop(){
digitalWrite (latchPin, LOW);
digitalWrite (latchPin, HIGH); //capture the data on the input pins
for (x=0; x<8; x=x+1){
dataArray[x] = SPI.transfer(0); // send 0 out on MOSI while reading in on MISO
}
// send to serial monitor
for (x=0; x<8; x=x+1){
Serial.println(dataArray[x]);
}
delay(3000); // chance to look at data
} // end loop

Thank you Sr, I will try it tomorrow morning.
BTW, is there a way to change data and clock pins for a MEGA256 board?

Yes. On Mega SCK is 52, MOSI is 51, MISO is 50.
Use 53 for latchPin.
You can use another, but 53 needs to be an output so the Mega is the SPI master.

CrossRoads:
Yes. On Mega SCK is 52, MOSI is 51, MISO is 50.
Use 53 for latchPin.
You can use another, but 53 needs to be an output so the Mega is the SPI master.
http://www.arduino.cc/en/uploads/Main/arduino-mega2560_R3-sch.pdf

Thank you!
Those pins can't be customized, do they?
What happen if I want 2 different sets of chips in the same board?

You can bit-bang SPI. Some details here: Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino

Jay98:
Those pins can't be customized, do they?

Hardware SPI pins cannot be changed or customized.

Thanks a lot