/*
  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);
 
}
74HC595
74HC165