/*
Copyright (c) 2023 Devansh Shukla R
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Define Connections to 74HC165
int load = 47; // PL pin 1
int clockEnablePin = 19; // CE pin 15
int dataIn = 20; // Q7 pin 7
int clockIn = 21; // CP pin 2
// Array to store previous state of each pin
byte previousState[8] = {0};
void setup()
{
// Setup Serial Monitor
Serial.begin(9600);
// Setup 74HC165 connections
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(dataIn, INPUT);
}
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 only if state has changed
Serial.println("Pin States:");
for (int i = 0; i < 8; i++)
{
// Check if current state is different from previous state
if ((incoming >> i & 1) != (previousState[i] >> i & 1))
{
Serial.print("Pin ");
Serial.print(i);
Serial.print(": ");
Serial.println(incoming >> i & 1);
// Update previous state
previousState[i] = incoming;
}
}
delay(200);
}