/*
*/
// Define Connections to 74HC595
 
// ST_CP pin 12
const int latchPin = 10;
// SH_CP pin 11
const int clockPin = 11;
// DS pin 14
const int dataPin = 12;

// Define Connections to 74HC165
// PL pin 1
int load = 7;
// CE pin 15
int clockEnablePin = 4;
// Q7 pin 7
int dataIn = 5;
// CP pin 2
int clockIn = 6;

int incomingDecimal;
int pointsLit = 0;

int P1R  = 0;
int P1G  = 0;
int P3R  = 0;
int P3G  = 0;
int P4R  = 0;
int P4G  = 0;
int P11R = 0;
int P11G = 0;

String P1="";
String P3="";
String P4="";
String P11="";

void setup()
{

  // Setup Serial Monitor
  Serial.begin(9600);

  // Setup 74HC165 connections
  pinMode(load, OUTPUT);
  pinMode(clockEnablePin, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);

  // Setup 74HC595 connections
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 255);
  digitalWrite(latchPin, HIGH);
  delay(500);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);
  digitalWrite(latchPin, HIGH);
  delay(500);
}

void loop()
{

  // 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);
  incomingDecimal = int(incoming);
  // Print to serial monitor
//  Serial.print("Pin States: ");
//  Serial.println(incomingDecimal, BIN);
  delay(100);

if (incoming != B11111111){

switch (incoming){

 
case B11111110:
  P1R = 1;
  P1G = 0;
  P1  = "Red";
  break;

case B11111101:
  P1R = 0;
  P1G = 2;
  P1  = "Grn";
  break;

case B11111011:
  P3R = 4;
  P3G = 0;
  P3  = "Red";
  break;

case B11110111:
  P3R = 0;
  P3G = 8;
  P3  = "Grn";
  break;
  
case B11101111:
  P4R = 16;
  P4G = 0;
  P4  = "Red";
  break;

case B11011111:
  P4R = 0;
  P4G = 32;
  P4  = "Grn";
  break;
   
case B1111111:
  P11R = 0;
  P11G = 128;
  P11="Grn";
  break;

case B10111111:
  P11R = 64;
  P11G = 0;
  P11  ="Red";
  break;

default:
  //
  break;

   }

pointsLit = P11G + P11R + P4G + P4R + P3G + P3R + P1G + P1R;


Serial.print("Keypress: ");
  Serial.print(incomingDecimal);
  Serial.print("  Shifting out: ");
  Serial.println(pointsLit);

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, pointsLit);
digitalWrite(latchPin, HIGH);
 
delay(100);
}
}
74HC165
74HC595