#include <Arduino.h>
// Data pin for the shift register
const int dataPin = 11;
// Clock pin for the shift register
const int clockPin = 10;
// Latch pin for the shift register
const int latchPin = 9;
// Array to store the state of the outputs
// 0 = off, 1 = on
int outputs[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void setup() {
// Set the data, clock, and latch pins as output
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Set all outputs to off
updateShiftRegister();
}
void loop() {
// Turn on output 0
setOutput(0, 1);
delay(1000);
// Turn off output 0
setOutput(0, 0);
delay(1000);
// Turn on output 1
setOutput(1, 1);
delay(1000);
// Turn off output 1
setOutput(1, 0);
delay(1000);
}
// Function to set the state of a specific output
void setOutput(int output, int state) {
outputs[output] = state;
updateShiftRegister();
}
// Function to update the shift register with the current output state
void updateShiftRegister() {
// Set latch pin low to start the transmission
digitalWrite(latchPin, LOW);
// Shift out the data
for (int i = 7; i >= 0; i--) {
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, outputs[i]);
digitalWrite(clockPin, HIGH);
}
// Set latch pin high to latch the data
digitalWrite(latchPin, HIGH);
}