// Define the Arduino pins connected to the 74HC595
const int dataPin = 11; // DS (SER) - Serial Data Input
const int clockPin = 12; // SHCP (SRCLK) - Shift Register Clock
const int latchPin = 9; // STCP (RCLK) - Latch Pin
// We have 4 shift registers = 32 bits
const int numRegisters = 4;
byte registerState[numRegisters]; // Array to hold the state of each register
void setup() {
// Set the pins as outputs
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Initialize all outputs to 0 (LOW)
clearRegisters();
writeRegisters();
}
// Sets all register bits to LOW
void clearRegisters() {
for (int i = 0; i < numRegisters; i++) {
registerState[i] = 0;
}
}
// Writes the states from the registerState array to the shift registers
void writeRegisters() {
// Pull the latch pin LOW to start the update
digitalWrite(latchPin, LOW);
// Shift out the data, starting from the LAST register.
for (int i = numRegisters - 1; i >= 0; i--) {
shiftOut(dataPin, clockPin, MSBFIRST, registerState[i]);
}
// Pull the latch pin HIGH to update the outputs all at once
digitalWrite(latchPin, HIGH);
}
void loop() {
// Forward scan: 0 to 31
for (int i = 0; i < 32; i++) {
clearRegisters();
// Set only the current LED
int reg = i / 8;
int bit = i % 8;
registerState[reg] = (1 << bit);
writeRegisters();
delay(200); // Longer delay to see clearly
}
// Backward scan: 31 to 0
for (int i = 31; i >= 0; i--) {
clearRegisters();
// Set only the current LED
int reg = i / 8;
int bit = i % 8;
registerState[reg] = (1 << bit);
writeRegisters();
delay(200); // Longer delay to see clearly
}
}