/*
*/
#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,20,4);

// 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 turn1R = 13;
int turn1G = 12;
int turn2R = 11;
int turn2G = 10;
int turn3R = 9;
int turn3G = 8;
int turn4R = 3;
int turn4G = 2;
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(turn1R, OUTPUT);
pinMode(turn1G, OUTPUT);
pinMode(turn2R, OUTPUT);
pinMode(turn2G, OUTPUT);
pinMode(turn3R, OUTPUT);
pinMode(turn3G, OUTPUT);
pinMode(turn4R, OUTPUT);
pinMode(turn4G, OUTPUT);

  //pinMode(latchPin, OUTPUT);
  //pinMode(clockPin, OUTPUT);
  //pinMode(dataPin, OUTPUT);

  //shiftOut(dataPin, clockPin, LSBFIRST, 0);

  digitalWrite(turn1G, LOW);
  digitalWrite(turn1R, LOW);
  digitalWrite(turn2G, LOW);
  digitalWrite(turn2R, LOW);
  digitalWrite(turn3G, LOW);
  digitalWrite(turn3R, LOW);
  digitalWrite(turn4R, LOW);
  digitalWrite(turn4G, LOW);

 lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("Windmill  Hollow");
  lcd.setCursor(0,1);
  lcd.print("    Model trains");
  lcd.setCursor(0,2);
  lcd.print("Created by Rod & Mon");
  lcd.setCursor(0,3);
  lcd.print("Power to the oldies!");
  delay(1500);
  lcd.setCursor(0,2);
  lcd.print("                    ");
  lcd.setCursor(0,3);
  lcd.print("                    ");
  // lcd.setCursor(0,2);
  //lcd.print("Arduino LCM IIC 2004");
  // lcd.setCursor(2,3);
  //lcd.print("Power By Ec-yuan!");
  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);
    // Print to serial monitor
  //Serial.print("Pin States: ");
  //Serial.println(incoming, BIN);
  
  //delay(10);

switch (incoming){

case B11111110:
  digitalWrite(turn1R, HIGH);
  digitalWrite(turn1G, LOW);
  P1 = "Red";
  break;

case B11111101:
  digitalWrite(turn1G, HIGH);
  digitalWrite(turn1R, LOW);
  P1="Grn";
  break;

case B11111011:
  digitalWrite(turn2R, HIGH);
  digitalWrite(turn2G, LOW);
  P3="Red";
  break;

case B11110111:
  digitalWrite(turn2G, HIGH);
  digitalWrite(turn2R, LOW);
  P3="Grn";
  break;
  
case B11101111:
  digitalWrite(turn3R, HIGH);
  digitalWrite(turn3G, LOW);
  P4="Red";
  break;

case B11011111:
  digitalWrite(turn3G, HIGH);
  digitalWrite(turn3R, LOW);
  P4="Grn";
  break;

   
case B10111111:
  digitalWrite(turn4R, HIGH);
  digitalWrite(turn4G, LOW);
  P11="Red";
  break;

case B01111111:
  digitalWrite(turn4G, HIGH);
  digitalWrite(turn4R, LOW);
  P11="Grn";
  break;
 
  }
 lcd.setCursor(0,2);
  lcd.print("P1 = ");
  lcd.print(P1);
 lcd.setCursor(11,2);
  lcd.print("P3 = ");
  lcd.print(P3);
 lcd.setCursor(0,3);
  lcd.print("P4 = ");
  lcd.print(P4);
 lcd.setCursor(10,3);
  lcd.print("P11 = ");
  lcd.print(P11);
  
  //delay(10);

}
74HC165