// Pin connections to the shift registers
const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 11;
const int buttonPin = 7; // Pin connected to the button
const int ledSwitchPin = 6; // Pin connected to the LED control switch
// Array to store continuity data
byte continuityData[8];
void setup() {
// Set the control pins to be outputs
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Button input with internal pull-up
pinMode(ledSwitchPin, INPUT_PULLUP); // LED control switch input with internal pull-up
Serial.begin(9600);
}
void loop() {
// Read continuity from SR1 into the array
readContinuityData();
// Check the state of the LED control switch
if (digitalRead(ledSwitchPin) == LOW) {
// LED switch is ON, update LEDs based on the continuity data
updateLEDs();
} else {
// LED switch is OFF, turn off all LEDs
turnOffLEDs();
}
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Button is pressed, activate corresponding pins
activatePins();
}
delay(1000); // Update every second
}
void readContinuityData() {
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
byte incoming = shiftIn(dataPin, clockPin, MSBFIRST);
// Store the continuity state in the array
for (int i = 0; i < 8; i++) {
continuityData[i] = bitRead(incoming, i);
}
}
void updateLEDs() {
byte redLEDState = 0;
byte greenLEDState = 0;
for (int i = 0; i < 8; i++) {
if (continuityData[i] == 1) {
bitSet(greenLEDState, i);
} else {
bitSet(redLEDState, i);
}
}
// Update the shift registers with the LED states
writeShiftRegister(redLEDState, greenLEDState);
}
void turnOffLEDs() {
// Turn off all LEDs by writing 0 to the shift registers controlling the LEDs
writeShiftRegister(0, 0);
}
void activatePins() {
byte outputState = 0;
for (int i = 0; i < 8; i++) {
if (continuityData[i] == 1) {
bitSet(outputState, i);
writeSingleShiftRegister(outputState);
delay(200);
bitClear(outputState, i);
}
}
}
void writeShiftRegister(byte redLEDState, byte greenLEDState) {
digitalWrite(latchPin, LOW);
// Shift out the red LED state
shiftOut(dataPin, clockPin, MSBFIRST, redLEDState);
// Shift out the green LED state
shiftOut(dataPin, clockPin, MSBFIRST, greenLEDState);
digitalWrite(latchPin, HIGH);
}
void writeSingleShiftRegister(byte outputState) {
digitalWrite(latchPin, LOW);
// Shift out the output state
shiftOut(dataPin, clockPin, MSBFIRST, outputState);
digitalWrite(latchPin, HIGH);
}