#include <Wire.h>
#include<LiquidCrystal_I2C.h>


// set the LCD number of columns and rows
int lcdColumns = 20;
int lcdRows = 4;

// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, lcdColumns, lcdRows);  


const int latchPin = 18;  /* PL - pin 1 */
const int clockPin = 19;  /* CP - pin 2*/
const int dataPin = 5;   /* Q7 - pin 7 */

const int clockenablePin = 25;

const int numBits = 8*1;   /* Set to 8 * number of shift registers */
byte nomor;
String os,rs;


char * int2bin(unsigned int x)
{
  static char buffer[17];
  for (int i=0; i<16; i++) buffer[15-i] = '0' + ((x & (1 << i)) > 0);
  buffer[16] ='\0';
  return buffer;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

 // Wire.begin();
  LCD.init();
  LCD.backlight();
  LCD.setCursor(0, 0);
  LCD.print("Welcome to Entry!");
  LCD.setCursor(0, 1);
  LCD.print("at your disposal! ");

  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockenablePin, OUTPUT);
  rs="";
}


void loop() {
 // Step 1: Sample
  digitalWrite(latchPin, LOW);
  delay(1);
  digitalWrite(latchPin, HIGH);
  delay(1);

  digitalWrite(clockPin, HIGH); // Shift out the next bit
  
    os=rs;
  rs="";

  digitalWrite(clockenablePin, LOW);
 int incom=shiftIn(dataPin,clockPin,MSBFIRST);
 digitalWrite(clockenablePin, HIGH);

rs=int2bin(incom);
  // Step 2: Shift

  int ir=0,jc=0;
  //Serial.print("Bits: ");
 /* for (int i = 0; i < numBits; i++) {
    int bit = digitalRead(dataPin);
    rs+=(bit == HIGH)?"1":"0";
    digitalWrite(clockPin, HIGH); // Shift out the next bit
    digitalWrite(clockPin, LOW);
  }*/
  if(os!=rs)
  {
     LCD.setCursor(0, 0);
     Serial.println(rs);
     LCD.print(rs);
  }
  delay(1000);
}
74HC165