// Define Connections to 74HC165
int latchPin = 15; // PL pin 1
int dataPin = 17; // Q7 pin 7
int clockPin = 16; // CP pin 2
int a[8];
byte incoming ;
void turn_on() {
// // Step 1: Sample
// digitalWrite(latchPin, LOW);
// digitalWrite(latchPin, HIGH);
// // Step 2: Shift
// // Serial.print("Bits: ");
// for (byte i = 0; i < 8; i++) {
// // byte bit = digitalRead(dataPin);
// a[i] = digitalRead(dataPin);
// Serial.print(a[i]);
// digitalWrite(clockPin, HIGH); // Shift out the next bit
// digitalWrite(clockPin, LOW);
// }
// Serial.println("");
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
incoming = shiftIn(dataPin, clockPin, MSBFIRST); //MSBFIRST //LSBFIRST
Serial.println(incoming);
}
void setup()
{
// Setup Serial Monitor
Serial.begin(115200);
// Setup 74HC165 connections
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
digitalWrite(latchPin, HIGH);
}
void loop()
{
// Write pulse to load pin
// digitalWrite(latchPin, LOW);
// digitalWrite(latchPin, HIGH);
// Get data from 74HC165
// digitalWrite(clockIn, HIGH); //這一行不要不然會錯
// byte incoming = shiftIn(dataPin, clockPin, MSBFIRST); //MSBFIRST //LSBFIRST
turn_on();
// Print to serial monitor
Serial.print("Pin States:\r\n");
// Serial.println(incoming, BIN);
Serial.println(incoming);
delay(500);
// // Step 1: Sample
// digitalWrite(latchPin, LOW);
// digitalWrite(latchPin, HIGH);
// // Step 2: Shift
// Serial.print("Bits: ");
// for (int i = 0; i < 8; i++) {
// int bit = digitalRead(dataPin);
// a[i]=digitalRead(dataPin);
// //Serial.print(a[i]);
// digitalWrite(clockPin, HIGH); // Shift out the next bit
// digitalWrite(clockPin, LOW);
// }
// Serial.print(a[0]);
// Serial.print(a[1]);
// Serial.print(a[2]);
// Serial.print(a[3]);
// Serial.print(a[4]);
// Serial.print(a[5]);
// Serial.print(a[6]);
// Serial.print(a[7]);
// Serial.println("");
// delay(200);
}