74HC166N 8-bit parallel-in/serial-out shift register

Used Products:

1. 74HC166N
2. 8 Position On/Off DIP Switch
3. Arduino UNO

Code:

#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); #define PIN_PL 8 #define PIN_CP 9 #define PIN_Q7 10 int j; int value; byte switchVar = 0; void setup() { lcd.begin(16, 2); Serial.begin(9600); pinMode(PIN_PL, OUTPUT); pinMode(PIN_CP, OUTPUT); pinMode(PIN_Q7, INPUT); } void loop() { byte dataIn = 0; digitalWrite(PIN_PL, 0); digitalWrite(PIN_CP, 0); digitalWrite(PIN_CP, 1); digitalWrite(PIN_PL, 1); for(j = 0; j < 8; j++) { value = digitalRead(PIN_Q7); if (value) { dataIn = dataIn | (1 << j); } digitalWrite(PIN_CP, LOW); digitalWrite(PIN_CP, HIGH); } if (switchVar != dataIn) { switchVar = dataIn; Serial.print("dataIn: "); Serial.print(dataIn, BIN); Serial.println(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("DEC: "+String(dataIn)); lcd.setCursor(0, 1); lcd.print("BIN: "+String(dataIn, BIN)); } }