#define clockPin 10 // Clock pin of 74HC595 is connected to Digital pin 10
#define dataPin 9 // Data pin of 74HC595 is connected to Digital pin 9
#define latchPin 8 // Latch pin of 74HC595 is connected to Digital pin 8
int numOfRegisters = 2;
byte* registerState;
long effectId = 0;
long prevEffect = 0;
long effectRepeat = 0;
long effectSpeed = 300; // Delay in milliseconds between LED changes
int currentLED = 0; // Keeps track of the currently lit LED
boolean allLedsOn = false;
void setup() {
// Initialize array
registerState = new byte[numOfRegisters];
for (size_t i = 0; i < numOfRegisters; i++) {
registerState[i] = 0;
}
// Set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Initialize Serial communication
Serial.begin(9600);
}
void loop() {
if (!allLedsOn) {
// Turn off all LEDs
for (int i = 0; i < 16; i++) {
bitClear(registerState[i / 8], i % 8);
}
updateShiftRegister();
delay(effectSpeed); // Delay for effectSpeed milliseconds
// Turn on the current LED
bitSet(registerState[currentLED / 8], currentLED % 8);
updateShiftRegister();
delay(effectSpeed); // Delay for effectSpeed milliseconds
// Move to the next LED
currentLED++;
// If we've reached LED 16, reset to LED 1
if (currentLED >= 16) {
currentLED = 0;
allLedsOn = true; // Set the flag to indicate all LEDs should be on next
}
} else {
// Turn on all LEDs
for (int i = 0; i < 16; i++) {
bitSet(registerState[i / 8], i % 8);
}
updateShiftRegister();
delay(effectSpeed); // Delay for effectSpeed milliseconds
allLedsOn = false; // Set the flag to go back to counting LEDs
}
}
void updateShiftRegister() {
// Send the data to the shift register
digitalWrite(latchPin, LOW);
for (int i = numOfRegisters - 1; i >= 0; i--) {
shiftOut(dataPin, clockPin, MSBFIRST, registerState[i]);
}
digitalWrite(latchPin, HIGH);
}