//2024-05-01
//untuk multi ic DS sambungnya ke Q7
//untuk ujung ic, bisa Q7 atau notQ7

/*
  74HC165 Shift register input example

  https://wokwi.com/arduino/projects/306031380875182657
  https://wokwi.com/projects/392708048017579009

  (C) 2021, Uri Shaked
*/

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

const int numBits = 8*2;   /* Set to 8 * number of shift registers */

byte nomor;

void setup() {
  Serial.begin(115200);
  delay(200);
  Serial.println("Start");
  // Serial.println("Please enter your name:");
  // while (!Serial.available()); // Wait for input
  // String name = Serial.readStringUntil('\n');
  // Serial.print("Hello, ");
  // Serial.print(name);
  // Serial.println("!");
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
}

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

  // Step 2: Shift
  //Serial.print("Bits: ");
  for (int i = 0; i < numBits; i++) {
    int bit = digitalRead(dataPin);
    if (bit == HIGH) {
      //Serial.print("1");
      //nomor = i+1; //aktif high
    } else {
      //Serial.print("0");
      nomor = i+1; //aktif low
    }
    digitalWrite(clockPin, HIGH); // Shift out the next bit
    digitalWrite(clockPin, LOW);
  }

  if (nomor != 0){
    Serial.print(" Tombol: ");
    Serial.println(String(nomor));
    nomor=0;
  }
  //Serial.println();
  //delay(1000);

  delay(200);
}
74HC165
74HC165