// This is not my project.
// I'm trying to fix it for someone on the Discord channel.
// Fixes:
// Software:
// make array for all the bits.
// I prefer latch pin for 165 default high.
// Pulse delay is now a variable.
// A positive clock pulse after reading a 165 bit.
// Circuit:
// Connect board GND to circuit GND.
// 165 Q7 goes to DS of the next one.
//74hc595 uscite
const int clockPin = 8;
const int latchPin = 9;
const int dataPin = 10;
// 74hc165 ingressi
const int REG_SL = 2;
const int REG_CK = 3;
const int REG_DI = 5;
// All 40 as bits in an array of 5 bytes.
// The Arduino Uno can work with a 64 bit number,
// but an array is easier.
//
// The highest bit is the green led on the left,
// and that is bit 7 of data[4].
// The lowest bit is the red led on the right,
// and that is bit 0 of data[0].
byte data[5];
const int pulseDelay = 2;
void setup()
{
Serial.begin(115200);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(REG_SL, OUTPUT);
digitalWrite(REG_SL, HIGH); // set default high
pinMode(REG_CK, OUTPUT); // default low
pinMode(REG_DI, INPUT);
}
void loop()
{
//acquisizione porte d'ingresso
digitalWrite(REG_SL, LOW);
delay(pulseDelay);
digitalWrite(REG_SL, HIGH);
delay(pulseDelay);
for(int i=4; i>=0; i--)
{
for (int j=0; j<8; j++)
{
int bit = digitalRead(REG_DI);
bitWrite(data[i],j,bit);
digitalWrite(REG_CK, HIGH);
delay(pulseDelay);
digitalWrite(REG_CK, LOW);
delay(pulseDelay);
}
}
printData();
digitalWrite(latchPin, LOW);
delay(pulseDelay);
for(int i=0; i<5; i++)
{
shiftOut(dataPin, clockPin, LSBFIRST, data[i]);
}
digitalWrite(latchPin, HIGH);
delay(pulseDelay);
}
// Print the 40 bits.
void printData()
{
for(int i=4; i>=0; i--) // index of array
{
for(int j=7; j>=0; j--) // walk the bits
{
int bit = bitRead(data[i],j);
if(bit == 0)
Serial.print("_"); // or "○"
else
Serial.print("#"); // or "●"
}
Serial.print(" ");
}
Serial.println();
}DS: dataPin (cyan)
SHCP: clockPin (violet)
STCP: latchPin (limegreen)
0
39
2
1
3
4
5
6
7
8
9
✨