Im trying to read inputs of the 2 74hc165 shift registers, however while some values are coming up as correct, sometimes some of bits dont properly respond to inputs. Im using an IR sensor to read inputs from it. its the TCRT5000. im using 2 on each registers to verify that its reading open and closed circuits as proper inputs. The values was will change to 1 if the IR sensor is connected, however when i move the input to another input pin, some of them are not properly updating or responding. some others do work properly. Is this a timing issue or a wiring issue? (Wiring information is on the line comments of the pin setup)
Thanks for any help!
int load = A0; // PL pin 1 of both registers
int clockEnablePin = A1; // CE pin 15 of both register
int dataIn = A3; // Q7 pin 9 of the second 74HC165, Pin 9 of first register goes to pin 10 of 2nd
int clockIn = A2; // CP pin 2 of both
byte inputData[2] = {B00000000, B00000000}; // Array to store the incoming data
void setup()
{
// Setup Serial Monitor
Serial.begin(9600);
// Setup 74HC165 connections
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(dataIn, INPUT);
}
void loop()
{
// Load parallel data into the shift registers
digitalWrite(load, LOW);
delayMicroseconds(10);
digitalWrite(load, HIGH);
delayMicroseconds(10);
// Enable the clock and read the data into the array
digitalWrite(clockEnablePin, LOW);
digitalWrite(clockIn, HIGH);
inputData[0] = shiftIn(dataIn, clockIn, LSBFIRST); // Read 8 bits from the first shift register
inputData[1] = shiftIn(dataIn, clockIn, LSBFIRST); // Read 8 bits from the second shift register
digitalWrite(clockEnablePin, HIGH);
// Print the data with all 8 bits
Serial.println("Shift Register States:");
Serial.print("Register 1: ");
printBinary8(inputData[0]); // Print the first register's data as 8-bit binary
Serial.print("Register 2: ");
printBinary8(inputData[1]); // Print the second register's data as 8-bit binary
delay(200); // Delay for readability
}
// Function to print a byte as 8-bit binary
void printBinary8(byte value)
{
for (int i = 7; i >= 0; i--) {
Serial.print((value >> i) & 1); // Print each bit
}
Serial.println(); // Newline for the next print
}