const uint8_t SPinB[] = {6, 5, 4, 3};
const uint8_t SPinL[] = {11, 10, 9, 8};

const int SIGB = 2; // SIG pin
const int SIGL = 7; // SIG pin

int readMux(int channel) {
  for (int i = 0; i < 4; i++)
    digitalWrite(SPinB[i], channel >> i & 1);

  delay(10);
  return digitalRead(SIGB);
}

void setup() {
  Serial.begin(115200);

  pinMode(SIGB, INPUT);
  pinMode(SIGL, OUTPUT);

  for (int i = 0; i < 4; i++) {
    pinMode(SPinB[i], OUTPUT);
    pinMode(SPinL[i], OUTPUT);
  }
}

void loop() {
  // Print it to the Serial Monitor
  for (int i = 0; i < 16; i++) {
    Serial.print(readMux(i));
  }
  Serial.println();

  // Find the first pressed button
  bool found = false;
  int index = 0;
  for (int i = 0; i < 16 && !found; i++) {
    if (readMux(i) == LOW) {
      index = i;
      found = true;
    }
  }

  if (found) {
    // Found a pressed button ?
    // The diagram has the buttons in a different order, therefor it is 15-index
    writeMux(index);
    digitalWrite(SIGL, HIGH);
  } else {
    // Nothing found? Then turn off all leds
    // The Enable is not used, so just set the SIG to LOW.
    digitalWrite(SIGL, LOW);
  }
  delay(50);
}

void writeMux(int channel) {
  for (int i = 0; i < 4; i++) {
    digitalWrite(SPinL[i], channel >> i & 1);
    // Serial.println(SPinL[i]);
    //Serial.println(channel);
  }
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
Loading
cd74hc4067
Loading
cd74hc4067
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay2:VCC
relay2:GND
relay2:IN
relay2:NC
relay2:COM
relay2:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay3:VCC
relay3:GND
relay3:IN
relay3:NC
relay3:COM
relay3:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay4:VCC
relay4:GND
relay4:IN
relay4:NC
relay4:COM
relay4:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay5:VCC
relay5:GND
relay5:IN
relay5:NC
relay5:COM
relay5:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay6:VCC
relay6:GND
relay6:IN
relay6:NC
relay6:COM
relay6:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay7:VCC
relay7:GND
relay7:IN
relay7:NC
relay7:COM
relay7:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay8:VCC
relay8:GND
relay8:IN
relay8:NC
relay8:COM
relay8:NO
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
btn5:1.l
btn5:2.l
btn5:1.r
btn5:2.r
btn6:1.l
btn6:2.l
btn6:1.r
btn6:2.r
btn7:1.l
btn7:2.l
btn7:1.r
btn7:2.r
btn8:1.l
btn8:2.l
btn8:1.r
btn8:2.r
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2
r6:1
r6:2
r7:1
r7:2
r8:1
r8:2
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
led4:A
led4:C
led5:A
led5:C
led6:A
led6:C
led7:A
led7:C
led8:A
led8:C
NOCOMNCVCCGNDINLED1PWRRelay Module
relay9:VCC
relay9:GND
relay9:IN
relay9:NC
relay9:COM
relay9:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay10:VCC
relay10:GND
relay10:IN
relay10:NC
relay10:COM
relay10:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay11:VCC
relay11:GND
relay11:IN
relay11:NC
relay11:COM
relay11:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay12:VCC
relay12:GND
relay12:IN
relay12:NC
relay12:COM
relay12:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay13:VCC
relay13:GND
relay13:IN
relay13:NC
relay13:COM
relay13:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay14:VCC
relay14:GND
relay14:IN
relay14:NC
relay14:COM
relay14:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay15:VCC
relay15:GND
relay15:IN
relay15:NC
relay15:COM
relay15:NO
NOCOMNCVCCGNDINLED1PWRRelay Module
relay16:VCC
relay16:GND
relay16:IN
relay16:NC
relay16:COM
relay16:NO
btn9:1.l
btn9:2.l
btn9:1.r
btn9:2.r
btn10:1.l
btn10:2.l
btn10:1.r
btn10:2.r
btn11:1.l
btn11:2.l
btn11:1.r
btn11:2.r
btn12:1.l
btn12:2.l
btn12:1.r
btn12:2.r
btn13:1.l
btn13:2.l
btn13:1.r
btn13:2.r
btn14:1.l
btn14:2.l
btn14:1.r
btn14:2.r
btn15:1.l
btn15:2.l
btn15:1.r
btn15:2.r
btn16:1.l
btn16:2.l
btn16:1.r
btn16:2.r
r9:1
r9:2
r10:1
r10:2
r11:1
r11:2
r12:1
r12:2
r13:1
r13:2
r14:1
r14:2
r15:1
r15:2
r16:1
r16:2
led9:A
led9:C
led10:A
led10:C
led11:A
led11:C
led12:A
led12:C
led13:A
led13:C
led14:A
led14:C
led15:A
led15:C
led16:A
led16:C
led17:A
led17:C
led18:A
led18:C
led19:A
led19:C
led20:A
led20:C
led21:A
led21:C
led22:A
led22:C
led23:A
led23:C
led24:A
led24:C
led25:A
led25:C
led26:A
led26:C
led27:A
led27:C
led28:A
led28:C
led29:A
led29:C
led30:A
led30:C
led31:A
led31:C
led32:A
led32:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r1:1
r1:2
r17:1
r17:2
r18:1
r18:2
r19:1
r19:2
r20:1
r20:2
r21:1
r21:2
r22:1
r22:2
r23:1
r23:2
r24:1
r24:2
r25:1
r25:2
r26:1
r26:2
r27:1
r27:2
r28:1
r28:2
r29:1
r29:2
r30:1
r30:2
r31:1
r31:2
r32:1
r32:2