/*
  74HC595 & 74HC165 Shift Register Demonstration
  74hc595-to-74ch165.ino
  Input for 8 pushbuttons using 74HC165
  Output to 0 LEDs using 74HC595
 
  DroneBot Workshop 2020
  https://dronebotworkshop.com
*/
 // int RCLKPin = 19;//21;   // pin 12 on the 74hc595 latch - nSS
// int SRCLKPin = 23;       // pin 11 on the 74hc595 shift register clock - SCK
// int SERPin = 18;//22;   // pin 14 on the 74hc595 data - MOSI
// Define Connections to 74HC165
//const int dataPin = 32;   /* Q7 */
//const int clockPin = 26;  /* CP */
//const int latchPin = 33;  /* PL */
 
// PL pin 1
int load = 33;
// CE pin 15
int clockEnablePin = 25;
// Q7 pin 7
int dataIn = 32;
// CP pin 2
int clockIn = 26;
 
// Define Connections to 74HC595
 
// ST_CP pin 12
const int latchPin = 19;
// SH_CP pin 11
const int clockPin = 23;
// DS pin 14
const int dataPin = 18;
 
void setup () {
 
  // Setup Serial Monitor
  Serial.begin(9600);
 
  // 74HC165 pins
  pinMode(load, OUTPUT);
  pinMode(clockEnablePin, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);
 
  // 74HC595 pins
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
 
}
 
 

 
void loop() {// Read Switches
 
  // Write pulse to load pin
  digitalWrite(load, LOW);
  delayMicroseconds(5);
  digitalWrite(load, HIGH);
  delayMicroseconds(5);
 
  // Get data from 74HC165
  digitalWrite(clockIn, HIGH);
  digitalWrite(clockEnablePin, LOW);
  byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
  digitalWrite(clockEnablePin, HIGH);
 
  // Print to serial monitor
  Serial.print("Pin States:\r\n");
  Serial.println(incoming, BIN);
 
 
  // Write to LEDs
 
  // ST_CP LOW to keep LEDs from changing while reading serial data
  digitalWrite(latchPin, LOW);
 
  // Shift out the bits
  shiftOut(dataPin, clockPin, LSBFIRST, ~incoming);
 
  // ST_CP HIGH change LEDs
  digitalWrite(latchPin, HIGH);
 
  delay(500);
 
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
74HC595
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
led2:A
led2:C
r2:1
r2:2
led1:A
led1:C
r1:1
r1:2
led3:A
led3:C
r3:1
r3:2
led4:A
led4:C
r4:1
r4:2
led5:A
led5:C
r5:1
r5:2
led6:A
led6:C
r6:1
r6:2
led7:A
led7:C
r7:1
r7:2
led8:A
led8:C
r8:1
r8:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
74HC165
sr3:PL
sr3:CP
sr3:D4
sr3:D5
sr3:D6
sr3:D7
sr3:Q7_N
sr3:GND
sr3:Q7
sr3:DS
sr3:D0
sr3:D1
sr3:D2
sr3:D3
sr3:CE
sr3:VCC
r17:1
r17:2
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
r18:1
r18:2
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
r19:1
r19:2
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
r20:1
r20:2
btn5:1.l
btn5:2.l
btn5:1.r
btn5:2.r
r21:1
r21:2
btn6:1.l
btn6:2.l
btn6:1.r
btn6:2.r
r22:1
r22:2
btn7:1.l
btn7:2.l
btn7:1.r
btn7:2.r
r23:1
r23:2
btn8:1.l
btn8:2.l
btn8:1.r
btn8:2.r
r24:1
r24:2