I'm trying to develop a project using shift registers. The idea is that you can connect output pins (using the 74HC595 shift register) to input pins (using the 74HC165 shift register) and Arduino will know which pin is connected to which.
So far I've come up with some code that theoretically looks good to me, but it doesn't really work. So it's either the code, the circuit, or both. So, here's the code
#include <SPI.h>
const byte inputLatch = 9;
const byte numOfInputChips = 1; // change this number if using more input chips
byte inputData[numOfInputChips] = { 0 }; // byte array to store data from input chips via SPI
byte oldInputData[numOfInputChips] = { 0 };
const byte outputLatch = 10;
const byte numOfOutputChips = 1; // change this number if using more output chips
const byte numOfOutputPins = numOfOutputChips * 8; // 8 if using 8bit shift out registers
byte outputData[numOfOutputChips] = { 0 }; // byte array to transfer data to output chips via SPI
const byte numOfTransferData = numOfOutputPins * numOfInputChips; // number of bytes to be transfered to Pure Data over serial
byte connectionData[numOfTransferData + 1]; // buffer to hold data transfered to Pure Data over serial
void refreshOutput(int chip)
{
digitalWrite(outputLatch, LOW);
SPI.transfer(outputData[chip]);
digitalWrite(outputLatch, HIGH);
}
void refreshInput(int chip)
{
digitalWrite(inputLatch, LOW);
digitalWrite(inputLatch, HIGH);
inputData[chip] = SPI.transfer(0);
}
void setup()
{
SPI.begin();
pinMode(inputLatch, OUTPUT);
pinMode(outputLatch, OUTPUT);
digitalWrite(inputLatch, HIGH);
digitalWrite(outputLatch, HIGH);
for(int i = numOfOutputChips - 1; i >= 0; i--){
outputData[i] = 0;
refreshOutput(i);
}
Serial.begin(115200);
}
void loop()
{
connectionData[0] = 0xc0; // denote start of data stream
byte index = 1; // buffer index offset
for(int k = 0; k < numOfOutputChips; k++){ // run through the output chips
for(int i = 0; i < 8; i++){ // run through the pins of each chip
bitWrite(outputData[k], i, 1); // set current output pin high, the rest are low
refreshOutput(k); // send byte to output chip via SPI
for(int j = 0; j < numOfInputChips; j++){ // read through the input chips
refreshInput(j); // receive byte from input chip via SPI
connectionData[index++] = inputData[j]; // write input byte to buffer
}
delay(1);
bitWrite(outputData[k], i, 0); // reset output pin low
refreshOutput(k);
}
if(inputData[k] != oldInputData[k])
Serial.write(connectionData, numOfTransferData + 1);
oldInputData[k] = inputData[k];
}
}
What happens is that I'm setting high the output pins one by one, while the rest are low, and I'm reading the input pins and checking if the current output pin is connected to any of the input pins (I'm using jumper wires on a breadboard to connect the pins). My initial idea is to replace, say a push button, with the connection between an output and an input pin. So, I'm grounding the input pins with a 10k resistor, and thought that connecting it to an output pin, if the output pin is low, the input pin will read low, if the output pin is high, the input pin will read high. I want to collect a byte for each output pin, that will represent this pins connections to an input chip (so, one byte for an output pin and an input chip), which byte I will afterwards translate inside Pure Data. But it doesn't really work. Can anyone give some help here?