// PL pin 1
int load = 4;
// CE pin 15
int clockEnablePin = 5;
// Q7 pin 7
int dataIn = 2;
// CP pin 2
int clockIn = 3;
uint8_t newByte = 0;
uint8_t lastByte = 5;
int16_t count = 0;
void setup() {
Serial.begin(115200);
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(dataIn, INPUT);
}
void loop() {
digitalWrite(load, LOW);
delayMicroseconds(5);
digitalWrite(load, HIGH);
delayMicroseconds(5);
for (int i = 0; i < 2; i++) {
if (digitalRead(dataIn) == 1) {
bitSet(newByte, i);
}
else {
bitClear(newByte, i);
}
digitalWrite(clockIn, HIGH); // Shift out the next bit
delayMicroseconds(10);
digitalWrite(clockIn, LOW);
}
if (newByte != lastByte) {
Serial.print(newByte);
if (lastByte == 0) {
if (newByte == 1) Serial.println(" CW");
if (newByte == 2) Serial.println(" CCW");
} else {Serial.println();}
if (newByte == 0) {
if (lastByte == 2) count ++;
if (lastByte == 1) count --;
Serial.print("c = ");
Serial.println(count);
}
lastByte = newByte;
}
newByte = 0;
delay(1);
}