I'm trying to get my 74hc595 working with Teensy 4.1, I couldn't find any examples of this so I got ChatGPT to write the code for me, I'm not sure if I've wired it up wrong or the code is wrong but it is not registering button presses, the serial.prints are showing all buttons as 0 even when I press one.
C++:
#include <SPI.h>
const int latchPin = 10; // Connects to Pin 12 (Latch/ST_CP) on 74HC595
const int dataPin = 11; // Connects to Pin 14 (Data/SI) on 74HC595
const int clockPin = 13; // Connects to Pin 11 (Clock/SH_CP) on 74HC595
void setup() {
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
// Read button states
byte buttonsState = readButtons();
// Print button states
Serial.print("Button States: ");
for (int i = 0; i < 8; ++i) {
Serial.print(bitRead(buttonsState, i));
}
Serial.println();
delay(500); // Add a delay to avoid fast button state changes
}
byte readButtons() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, B11111111); // Shift in all 8 bits
digitalWrite(latchPin, HIGH);
return digitalRead(0); // Replace 0 with the appropriate pin number where the Q0 is connected
}