// amount of used 74HC165 chips
#define AMOUNT_IC 1
#define DATA_WIDTH AMOUNT_IC * 8
const uint8_t DataPin = 2; // 74HC595 SER (14) pin
const uint8_t LatchPin = 3; // 74HC595 RCLK (12) pin
const uint8_t ClockPin = 4; // 74HC595 SRCLK (11) pin
#define PIN_PL 8 // 74HC165 latch (1) pin
#define PIN_CE 5 // 74HC165 clock enable (13) pin
#define PIN_Q7 6 // 74HC165 data (9) pin
#define PIN_CLK 7 // 74HC165 clock (2) pin
unsigned long pinValues, oldPinValues;
void setup() {
Serial.begin(9600);
// 74HC165 connections
pinMode(PIN_PL, OUTPUT);
pinMode(PIN_CE, OUTPUT);
pinMode(PIN_CLK, OUTPUT);
pinMode(PIN_Q7, INPUT);
// 74HC595 connections
pinMode(LatchPin, OUTPUT);
pinMode(DataPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
digitalWrite(PIN_CLK, LOW);
digitalWrite(PIN_PL, HIGH);
digitalWrite(LatchPin, HIGH);
pinValues = readShiftRegisters();
printByte();
oldPinValues = pinValues;
}
void loop() {
pinValues = readShiftRegisters();
byte ledData = 0;
for (byte i = 0; i < DATA_WIDTH; i++) {
if (pinValues & (1UL << i)) {
ledData |= (1 << i);
}
}
writeShiftRegister(ledData);
if (pinValues != oldPinValues) {
printByte();
oldPinValues = pinValues;
}
delay(500);
}
unsigned long readShiftRegisters() {
long bitVal;
unsigned long bytesVal = 0;
digitalWrite(PIN_CE, HIGH);
digitalWrite(PIN_PL, LOW);
delayMicroseconds(5);
digitalWrite(PIN_PL, HIGH);
digitalWrite(PIN_CE, LOW);
for (byte i = 0; i < DATA_WIDTH; i++) {
bitVal = digitalRead(PIN_Q7);
bytesVal |= (bitVal << ((DATA_WIDTH - 1) - i));
digitalWrite(PIN_CLK, HIGH);
delayMicroseconds(5);
digitalWrite(PIN_CLK, LOW);
}
return (bytesVal);
}
void writeShiftRegister(byte data) {
digitalWrite(LatchPin, LOW);
shiftOut(DataPin, ClockPin, MSBFIRST, data);
digitalWrite(LatchPin, HIGH);
}
void printByte() {
for (byte i = 0; i <= DATA_WIDTH - 1; i++) {
Serial.print("D" + String(i + 1) + " ");
}
Serial.println("");
for (byte i = 0; i <= DATA_WIDTH - 1; i++) {
Serial.print(pinValues >> i & 1, BIN);
Serial.print(" ");
}
Serial.println("\n");
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
sr1:PL
sr1:CP
sr1:D4
sr1:D5
sr1:D6
sr1:D7
sr1:Q7_N
sr1:GND
sr1:Q7
sr1:DS
sr1:D0
sr1:D1
sr1:D2
sr1:D3
sr1:CE
sr1:VCC
sr2:Q1
sr2:Q2
sr2:Q3
sr2:Q4
sr2:Q5
sr2:Q6
sr2:Q7
sr2:GND
sr2:Q7S
sr2:MR
sr2:SHCP
sr2:STCP
sr2:OE
sr2:DS
sr2:Q0
sr2:VCC
sw1:1a
sw1:2a
sw1:3a
sw1:4a
sw1:5a
sw1:6a
sw1:7a
sw1:8a
sw1:8b
sw1:7b
sw1:6b
sw1:5b
sw1:4b
sw1:3b
sw1:2b
sw1:1b
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
led4:A
led4:C
led5:A
led5:C
led6:A
led6:C
led7:A
led7:C
led8:A
led8:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2
r6:1
r6:2
r7:1
r7:2
r8:1
r8:2
r9:1
r9:2
r10:1
r10:2
r11:1
r11:2
r12:1
r12:2
r13:1
r13:2
r14:1
r14:2
r15:1
r15:2
r16:1
r16:2