//Shift Register 74HC165
//シフトレジスタ74HC165の使い方
//Copyright (C) Oz@SIT all rights reserved.
//Originally brought from https://dronebotworkshop.com/shift-registers/#Arduino_74HC165_Sketch
//rev 0.1 2022/12/31

/*
 * 74HC165
 * SH/LD 7
 * CLK 6
 * QH 8
 */
 
int const SL = 7;
int const CLK = 6;
int const SER = 8;

/*
  74HC165 Shift Register Demonstration 1
  74hc165-demo.ino
  Read from 8 dipswitches and display values on serial monitor

  DroneBot Workshop 2020
  https://dronebotworkshop.com
*/

// Define Connections to 74HC165

// PL pin 1
int load = 7;   //load pinを7ピンに設定
// CE pin 15
int clockInhibit = 5;  //clockInhibit pinを5ピンに設定
// Q7 pin 9
int dataIn = 8;   //data pinを8ピンに設定
// CP pin 2
int clockIn = 6;  //clock pinを6ピンに設定

void setup()
{
  // Setup Serial Monitor
  Serial.begin(9600);

  // Setup 74HC165 connections
  //デジタルピンの入出力の設定
  pinMode(load, OUTPUT);
  pinMode(clockInhibit, OUTPUT);
  pinMode(clockIn, OUTPUT);
  pinMode(dataIn, INPUT);
}

void loop()
{
  // Write pulse to load pin
  //loadピンにLOW→HIGHのパルスを入れてデータをロードする。
  digitalWrite(load, LOW);
  delayMicroseconds(5);
  digitalWrite(load, HIGH);
  delayMicroseconds(5);

  // Get data from 74HC165
  //データを1ビットずつ読み込む。そのためclockIn
  digitalWrite(clockIn, HIGH);
  digitalWrite(clockInhibit, LOW);
  byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
  digitalWrite(clockInhibit, HIGH);

  // Print to serial monitor
  Serial.print("Pin States: ");
  Serial.println(incoming, BIN);
  delay(200);
}
$abcdeabcde151015202530fghijfghij
74HC165