inputs to arduino through 74HC165 **FIXED**

Hi all! Hope you can help with a little problem I have receiving button presses through a 74HC165 shift register.

The code is as follows:

//--------------------------------------------------------------------------------------------------------------------------------
//button test routine
//--------------------------------------------------------------------------------------------------------------------------------
//Initialise standard vars below

int counter = 0;
int checkState;
int pressedFlag;
int feedBack = 0;
int whichSwitch = 0;

int trafficPin = 13;

int slPin = 12;
int clkPin = 9;
int dPin = 11;

int switchCycleArray[9] = {0,0,0,0,0,0,0,0};

void setup(){

pinMode(trafficPin,OUTPUT);
digitalWrite(trafficPin,HIGH);

pinMode(slPin, OUTPUT);
pinMode(clkPin, OUTPUT);
pinMode(dPin, INPUT);

Serial.begin(57600);
delay(100);
Serial.println("—waiting for switch press—");

}

void loop(){
funcSwitchListen();
delay(100);
}

//--------------------------------------------------------------------------------------------------------------------------------
//The function below runs the switch testing loop on the 74HC165 8bit shift register demuxing IC
//--------------------------------------------------------------------------------------------------------------------------------
// 1. The switch/load pin is made low to read the inputs into the shift register
// 2. The switch/load pin is then made high
// 3. pulseIn is read and the clock is cycled to advance to the next register
// 4. When the 8 registers have been read, the function cycles through them to determine whether any of the switches have been hit
// 5. The register number is printed to the serial out
//--------------------------------------------------------------------------------------------------------------------------------

int funcSwitchListen(){

//pulse the switch/load pin low to grab the register set
digitalWrite(clkPin,LOW);
digitalWrite(slPin, LOW);
digitalWrite(slPin, HIGH);

//populate the array here
for(counter=0; counter<8; counter++){

//add the state to the array
checkState = pulseIn(dPin, HIGH);
Serial.print(checkState);

if(checkState>0){
switchCycleArray[counter] = 1;
} else {
switchCycleArray[counter] = 0;
}
//set clock pin high for next register cycle
digitalWrite(clkPin,HIGH);
digitalWrite(clkPin,LOW);

}

//--------------------------------------------------------
//got a switch register array filled, check for any highs
//--------------------------------------------------------

//reset the pressed switch flag state to 0
pressedFlag = 0;

for (whichSwitch=0; whichSwitch<8; whichSwitch++){

//check which clock cycle went high
if(switchCycleArray[whichSwitch]==1){
feedBack = switchCycleArray[whichSwitch];
pressedFlag = 1;
}

//something’s been pressed!
if(pressedFlag == 1){
//dump out the switch reference
Serial.print(">> “);
Serial.print(feedBack);
Serial.println(” <<");
}
}
//time to start again…

}

I am having to make pin13 high as this is required for another function of the completed project. After the waiting… prompt i don’t receive any feedback from any of the button presses.

I have looked at the other shift register tutorials but they don’t seem to be much help with this particular issue (unless i am missing something!)

Hopefully someone has an answer or can point me in the right direction

best wishes, Caleb

ahhh, I'm such a n00b! i switched the pulseIn for a digitalRead and it works :) also to note that the read is 1, unless the button is pressed when it goes 0