// 74HC165 pins
const byte inputSelectPin = 2;
const byte clockPulsePin = 3;
const byte dataOutPin = 4;
// 74HC595 pins
const byte clearPin = 5;
const byte serialDataPin = 6;
const byte shiftClockPin = 7;
const byte latchClockPin = 8;
// Number of input buttons (adjust as needed)
const int numButtons = 8;
uint16_t inputData = 0; // Store input data
uint16_t previousData = 0; // Store the previous input data
byte mode = 0; // Store the selected mode: 0 for SIPO, 1 for PISO, 2 for PIPO, 3 for SISO
void setup() {
// 74HC165 setup
pinMode(inputSelectPin, OUTPUT);
pinMode(clockPulsePin, OUTPUT);
pinMode(dataOutPin, INPUT);
// 74HC595 setup
pinMode(clearPin, OUTPUT);
pinMode(shiftClockPin, OUTPUT);
pinMode(latchClockPin, OUTPUT);
pinMode(serialDataPin, OUTPUT);
digitalWrite(clearPin, LOW);
digitalWrite(clearPin, HIGH);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read input data from 74HC165
inputData = readInputData();
// Update the 74HC595 with the new input data
updateOutputData(inputData);
// Read mode selection from DIP switch (1 to 4)
mode = readModeSelection();
// Print data and mode for debugging
printData(inputData);
printMode(mode);
// Delay before the next loop iteration
delay(5000);
}
// Function to read input data from 74HC165
uint16_t readInputData() {
uint16_t dataIn = 0;
digitalWrite(inputSelectPin, LOW);
digitalWrite(clockPulsePin, LOW);
digitalWrite(clockPulsePin, HIGH);
digitalWrite(inputSelectPin, HIGH);
for (int j = 0; j < numButtons; j++) {
bool buttonState = digitalRead(dataOutPin);
dataIn |= (buttonState << j);
digitalWrite(clockPulsePin, LOW);
digitalWrite(clockPulsePin, HIGH);
}
return dataIn;
}
// Function to update 74HC595 with input data
void updateOutputData(uint16_t data) {
digitalWrite(latchClockPin, LOW);
shiftOut(serialDataPin, shiftClockPin, MSBFIRST, highByte(data));
shiftOut(serialDataPin, shiftClockPin, MSBFIRST, lowByte(data));
digitalWrite(latchClockPin, HIGH);
}
// Function to read mode selection from DIP switch (1 to 4)
byte readModeSelection() {
// Use pins 9 to 12 for mode selection (1 to 4)
byte modeSelection = 0;
for (byte i = 9; i <= 12; i++) {
modeSelection |= (digitalRead(i) << (i - 9));
}
return modeSelection;
}
// Function to print data for debugging
void printData(uint16_t data) {
if (previousData != data) {
previousData = data;
Serial.print("Input Data (DEC): ");
Serial.print(data, DEC);
Serial.println();
Serial.print("Input Data (BIN): ");
Serial.print(data, BIN);
Serial.println();
}
}
// Function to print the selected mode for debugging
void printMode(byte selectedMode) {
Serial.print("Selected Mode: ");
switch (selectedMode) {
case 0:
Serial.println("SIPO");
break;
case 1:
Serial.println("PISO");
break;
case 2:
Serial.println("PIPO");
break;
case 3:
Serial.println("SISO");
break;
default:
Serial.println("Invalid Mode");
break;
}
}