// --- PIN DEFINITIES ---
const int dataPin595 = 2;
const int latchPin595 = 3;
const int clockPin595 = 6;
const int dataPin165 = 5;
const int loadPin165 = 7;
const int clockPin165 = 8;
// LED-status (4 wissels → 8 LEDs)
bool ledStatus[8] = {false};
// ----------------------------------------------------
void setup() {
pinMode(dataPin595, OUTPUT);
pinMode(latchPin595, OUTPUT);
pinMode(clockPin595, OUTPUT);
pinMode(dataPin165, INPUT_PULLUP);
pinMode(loadPin165, OUTPUT);
pinMode(clockPin165, OUTPUT);
// Startstand: LED 1, 3, 5, 7 aan (rechtdoor)
for (int w = 0; w < 4; w++) {
ledStatus[w * 2] = true; // recht
ledStatus[w * 2 + 1] = false; // afbuigend
}
}
// ----------------------------------------------------
void loop() {
// --- 165 INLEZEN ---
digitalWrite(loadPin165, LOW);
delayMicroseconds(5);
digitalWrite(loadPin165, HIGH);
// BELANGRIJK: LSBFIRST ipv MSBFIRST
byte buttons = shiftIn(dataPin165, clockPin165, LSBFIRST);
// --- 4 WISSELS ONAFHANKELIJK VERWERKEN ---
// bit 0 = knop 1, bit 1 = knop 2, ... bit 7 = knop 8
for (int knop = 0; knop < 8; knop++) {
bool pressed = !(buttons & (1 << knop)); // actief laag
if (pressed) {
int wissel = knop / 2; // 0..3
bool afbuig = (knop % 2 == 1); // even = recht, oneven = afbuigend
int ledRecht = wissel * 2; // 0,2,4,6
int ledAf = ledRecht + 1; // 1,3,5,7
ledStatus[ledRecht] = !afbuig;
ledStatus[ledAf] = afbuig;
}
}
// --- 595 OUTPUT OPBOUWEN ---
// Q0 → LED1, Q1 → LED2, ... Q7 → LED8
byte outByte = 0;
for (int i = 0; i < 8; i++) {
if (ledStatus[i]) outByte |= (1 << i);
}
digitalWrite(latchPin595, LOW);
shiftOut(dataPin595, clockPin595, MSBFIRST, outByte);
digitalWrite(latchPin595, HIGH);
delay(10);
}