12 daisy chained SN74HC165N

I'm trying to read 12 daisy chained SN74HC165Ns by modifying the code found in this example:

http://playground.arduino.cc/Code/ShiftRegSN74HC165N

However, I'm not sure what else I need to modify in the example code to make it work correctly.

The comment on line 15 says: /* * Of course you can daisy chain as many as you like while still * using only 4 Arduino pins (though you would have to process * them 4 at a time into separate unsigned long variables). */

I updated the example like this:

line 23: #define NUMBER_OF_SHIFT_CHIPS 12

line 40: #define BYTES_VAL_T unsigned long

That code will not work with more than two shift registers. I don't see how it is easly adapted. Maybe you could use an array to hold the results but it is way too big for a single variable.

Ok thanks. I'll try that. It worked well with 4 shift registers. So I think I would just need to store the bits in an array of 3 unsigned long variables?

Maybe by changing:

BYTES_VAL_T pinValues; BYTES_VAL_T oldPinValues;

to:

unsigned long pinValues[3]; unsigned long oldPinValues[3];

???

So I think I would just need to store the bits in an array of 3 unsigned long variables?

Yes.

unsigned long pinValues[3];
unsigned long oldPinValues[3];

Yes but don’t forget to change the variables in the rest of the code.

bobdev: Ok thanks. I'll try that. It worked well with 4 shift registers. So I think I would just need to store the bits in an array of 3 unsigned long variables?

You might find it easier to parse things if you used an array of 12 bytes - one for each shift register.

Really, the best arrangement would depend on what each shift register was reading; for instance, if each register was connected to something that was providing an 8-bit value (let's say 12 potentiometers, each into an 8 bit ADC) - then an array of bytes might be best - one byte per potentiometer.

However, if the ADC were a 16 bit device (so 6 potentiometers - each ADC feeding two '165 registers) - then an array of 6 integers might be better to represent the read values.

In either case, though, three unsigned longs - while still carrying the same information - might be more difficult (or at least more obscure) to parse at a later point in the development (also, the code would become less clear as to what and how it was working - which might lead to a future maintenance headache for your future self).

By keeping your data elements logically representative of the real-world components (and values), your code will become easier to read, understand, and maintain - both as you develop your project, and when you need to maintain it in the future...