// ===== Пины Arduino =====
const int dataPin = 4; // Q7 на 74HC165
const int clockPin = 5; // CP (Clock)
const int loadPin = 6; // PL / ~LD (Parallel Load)
// ===== Настройка =====
void setup() {
Serial.begin(9600); // монитор порта для вывода состояния кнопок
pinMode(dataPin, INPUT); // данные с регистра
pinMode(clockPin, OUTPUT);
pinMode(loadPin, OUTPUT);
digitalWrite(clockPin, LOW);
digitalWrite(loadPin, HIGH); // по умолчанию регистр в последовательном режиме
}
// ===== Функция считывания всех 8 кнопок =====
byte read8Buttons() {
byte buttons;
// 1) Параллельная загрузка состояния кнопок в регистр
digitalWrite(loadPin, LOW);
delayMicroseconds(5); // короткая пауза
digitalWrite(loadPin, HIGH);
// 2) Считываем 8 бит через shiftIn
buttons = shiftIn(dataPin, clockPin, LSBFIRST);
return buttons;
}
// ===== Главный цикл =====
void loop() {
byte buttons = read8Buttons();
// 1) Вывод состояния всех кнопок в бинарном формате
Serial.print("Buttons: ");
for (int i = 7; i >= 0; i--) { // бит 7 = H, бит 0 = A
Serial.print(bitRead(buttons, i));
}
Serial.println();
// 2) Проверка каждой кнопки
if (bitRead(buttons, 0)) Serial.println("Button A pressed");
if (bitRead(buttons, 1)) Serial.println("Button B pressed");
if (bitRead(buttons, 2)) Serial.println("Button C pressed");
if (bitRead(buttons, 3)) Serial.println("Button D pressed");
if (bitRead(buttons, 4)) Serial.println("Button E pressed");
if (bitRead(buttons, 5)) Serial.println("Button F pressed");
if (bitRead(buttons, 6)) Serial.println("Button G pressed");
if (bitRead(buttons, 7)) Serial.println("Button H pressed");
delay(200); // обновление каждые 200 мс
}