const byte dipPins[] = {9, 8, 7, 6, 5, 4, 3, 2};
void writeByte(const byte b) {
for (int8_t i = 7; i >= 0; i--) Serial.write(bitRead(b, i) + '0');
Serial.println();
// here we actually print a human readable version of the byte
// for binary communicatoin it shoud be
// Serial.write(b);
}
void setup() {
Serial.begin(115200);
for (byte aPin : dipPins) pinMode(aPin, INPUT_PULLUP);
}
void loop() {
static byte oldValue = 0x42;
byte currentValue = 0;
for (byte aPin : dipPins) {
currentValue <<= 1;
currentValue |= (digitalRead(aPin) == HIGH) ? 0 : 1; // INPUT_PULLUP => inverse input
}
if (currentValue != oldValue) {
writeByte(currentValue);
oldValue = currentValue;
}
}