// 74hc165  ingressi
const int REG_SL = 2; // limegreen
const int REG_CK = 3; // violet
const int REG_DI = 4; // cyan
// 74hc595 uscite
const int latchPin = 5; // limegreen
const int clockPin = 6; // violet
const int dataPin  = 7; // cyan
// 74hc165  ingressi 2
const int REG_SL2 = 8; // limegreen
const int REG_CK2 = 9; // violet
const int REG_DI2 = 10; // cyan
// 74hc595 uscite 2
const int latchPin2 = 11; // limegreen
const int clockPin2 = 12; // violet
const int dataPin2  = 13; // cyan

byte data[5];

const int pulseDelay = 2;

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

  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  pinMode(REG_SL, OUTPUT);
  digitalWrite(REG_SL, HIGH);  // set default high
  pinMode(REG_CK, OUTPUT);     // default low
  pinMode(REG_DI, INPUT);
}

void loop() {
  // acquisizione porte d'ingresso
  digitalWrite(REG_SL, LOW);
  delay(pulseDelay);
  digitalWrite(REG_SL, HIGH);
  delay(pulseDelay);
  for(int i=4; i>=0; i--) {
    for (int j=0; j<8; j++) {
      int bit = digitalRead(REG_DI);
      bitWrite(data[i],j,bit);

      digitalWrite(REG_CK, HIGH);
      delay(pulseDelay);
      digitalWrite(REG_CK, LOW);
      delay(pulseDelay);
    }
  }
  //printData();

  digitalWrite(latchPin, LOW);
  delay(pulseDelay);
  for(int i=0; i<5; i++) {
    shiftOut(dataPin, clockPin, LSBFIRST, data[i]);
  }
  digitalWrite(latchPin, HIGH);
  delay(pulseDelay);
}

/*
// Print the 40 bits.
void printData() {
  // index of array
  for(int i=4; i>=0; i--) {
  // walk the bits
    for(int j=7; j>=0; j--) {
      int bit = bitRead(data[i],j);
      if(bit == 0)
        Serial.print("○");  // or "_"
      else
        Serial.print("●");  // or "#"
    }
    Serial.print(" ");
  }
  Serial.println();
}
*/
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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sr11:D2
sr11:D3
sr11:CE
sr11:VCC
74HC165
sr12:PL
sr12:CP
sr12:D4
sr12:D5
sr12:D6
sr12:D7
sr12:Q7_N
sr12:GND
sr12:Q7
sr12:DS
sr12:D0
sr12:D1
sr12:D2
sr12:D3
sr12:CE
sr12:VCC
74HC165
sr13:PL
sr13:CP
sr13:D4
sr13:D5
sr13:D6
sr13:D7
sr13:Q7_N
sr13:GND
sr13:Q7
sr13:DS
sr13:D0
sr13:D1
sr13:D2
sr13:D3
sr13:CE
sr13:VCC
74HC165
sr14:PL
sr14:CP
sr14:D4
sr14:D5
sr14:D6
sr14:D7
sr14:Q7_N
sr14:GND
sr14:Q7
sr14:DS
sr14:D0
sr14:D1
sr14:D2
sr14:D3
sr14:CE
sr14:VCC
74HC165
sr15:PL
sr15:CP
sr15:D4
sr15:D5
sr15:D6
sr15:D7
sr15:Q7_N
sr15:GND
sr15:Q7
sr15:DS
sr15:D0
sr15:D1
sr15:D2
sr15:D3
sr15:CE
sr15:VCC
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
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
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
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
led33:A
led33:C
led34:A
led34:C
led35:A
led35:C
led36:A
led36:C
led37:A
led37:C
led38:A
led38:C
led39:A
led39:C
led40:A
led40:C
led41:A
led41:C
led42:A
led42:C
led43:A
led43:C
led44:A
led44:C
led45:A
led45:C
led46:A
led46:C
led47:A
led47:C
led48:A
led48:C
led49:A
led49:C
led50:A
led50:C
led51:A
led51:C
led52:A
led52:C
led53:A
led53:C
led54:A
led54:C
led55:A
led55:C
led56:A
led56:C
led57:A
led57:C
led58:A
led58:C
led59:A
led59:C
led60:A
led60:C
led61:A
led61:C
led62:A
led62:C
led63:A
led63:C
led64:A
led64:C
led65:A
led65:C
led66:A
led66:C
led67:A
led67:C
led68:A
led68:C
led69:A
led69:C
led70:A
led70:C
led71:A
led71:C
led72:A
led72:C
led73:A
led73:C
led74:A
led74:C
led75:A
led75:C
led76:A
led76:C
led77:A
led77:C
led78:A
led78:C
led79:A
led79:C
led80:A
led80:C
r1:1
r1:2
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
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
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
r33:1
r33:2
r34:1
r34:2
r35:1
r35:2
r36:1
r36:2
r37:1
r37:2
r38:1
r38:2
r39:1
r39:2
r40:1
r40:2
r41:1
r41:2
r42:1
r42:2
r43:1
r43:2
r44:1
r44:2
r45:1
r45:2
r46:1
r46:2
r47:1
r47:2
r48:1
r48:2
r49:1
r49:2
r50:1
r50:2
r51:1
r51:2
r52:1
r52:2
r53:1
r53:2
r54:1
r54:2
r55:1
r55:2
r56:1
r56:2
r57:1
r57:2
r58:1
r58:2
r59:1
r59:2
r60:1
r60:2
r61:1
r61:2
r62:1
r62:2
r63:1
r63:2
r64:1
r64:2
r65:1
r65:2
r66:1
r66:2
r67:1
r67:2
r68:1
r68:2
r69:1
r69:2
r70:1
r70:2
r71:1
r71:2
r72:1
r72:2
r73:1
r73:2
r74:1
r74:2
r75:1
r75:2
r76:1
r76:2
r77:1
r77:2
r78:1
r78:2
r79:1
r79:2
r80:1
r80:2
r81:1
r81:2
r82:1
r82:2
r83:1
r83:2
r84:1
r84:2
r85:1
r85:2
r86:1
r86:2
r87:1
r87:2
r88:1
r88:2
r89:1
r89:2
r90:1
r90:2
r91:1
r91:2
r92:1
r92:2
r93:1
r93:2
r94:1
r94:2
r95:1
r95:2
r96:1
r96:2
r97:1
r97:2
r98:1
r98:2
r99:1
r99:2
r100:1
r100:2
r101:1
r101:2
r102:1
r102:2
r103:1
r103:2
r104:1
r104:2
r105:1
r105:2
r106:1
r106:2
r107:1
r107:2
r108:1
r108:2
r109:1
r109:2
r110:1
r110:2
r111:1
r111:2
r112:1
r112:2
r113:1
r113:2
r114:1
r114:2
r115:1
r115:2
r116:1
r116:2
r117:1
r117:2
r118:1
r118:2
r119:1
r119:2
r120:1
r120:2
relay1:NO2
relay1:NC2
relay1:P2
relay1:COIL2
relay1:NO1
relay1:NC1
relay1:P1
relay1:COIL1
relay2:NO2
relay2:NC2
relay2:P2
relay2:COIL2
relay2:NO1
relay2:NC1
relay2:P1
relay2:COIL1
relay3:NO2
relay3:NC2
relay3:P2
relay3:COIL2
relay3:NO1
relay3:NC1
relay3:P1
relay3:COIL1
relay4:NO2
relay4:NC2
relay4:P2
relay4:COIL2
relay4:NO1
relay4:NC1
relay4:P1
relay4:COIL1
relay5:NO2
relay5:NC2
relay5:P2
relay5:COIL2
relay5:NO1
relay5:NC1
relay5:P1
relay5:COIL1
relay6:NO2
relay6:NC2
relay6:P2
relay6:COIL2
relay6:NO1
relay6:NC1
relay6:P1
relay6:COIL1
relay7:NO2
relay7:NC2
relay7:P2
relay7:COIL2
relay7:NO1
relay7:NC1
relay7:P1
relay7:COIL1
relay8:NO2
relay8:NC2
relay8:P2
relay8:COIL2
relay8:NO1
relay8:NC1
relay8:P1
relay8:COIL1
relay9:NO2
relay9:NC2
relay9:P2
relay9:COIL2
relay9:NO1
relay9:NC1
relay9:P1
relay9:COIL1
relay10:NO2
relay10:NC2
relay10:P2
relay10:COIL2
relay10:NO1
relay10:NC1
relay10:P1
relay10:COIL1
relay11:NO2
relay11:NC2
relay11:P2
relay11:COIL2
relay11:NO1
relay11:NC1
relay11:P1
relay11:COIL1
relay12:NO2
relay12:NC2
relay12:P2
relay12:COIL2
relay12:NO1
relay12:NC1
relay12:P1
relay12:COIL1
relay13:NO2
relay13:NC2
relay13:P2
relay13:COIL2
relay13:NO1
relay13:NC1
relay13:P1
relay13:COIL1
relay14:NO2
relay14:NC2
relay14:P2
relay14:COIL2
relay14:NO1
relay14:NC1
relay14:P1
relay14:COIL1
relay15:NO2
relay15:NC2
relay15:P2
relay15:COIL2
relay15:NO1
relay15:NC1
relay15:P1
relay15:COIL1
relay16:NO2
relay16:NC2
relay16:P2
relay16:COIL2
relay16:NO1
relay16:NC1
relay16:P1
relay16:COIL1
relay17:NO2
relay17:NC2
relay17:P2
relay17:COIL2
relay17:NO1
relay17:NC1
relay17:P1
relay17:COIL1
relay18:NO2
relay18:NC2
relay18:P2
relay18:COIL2
relay18:NO1
relay18:NC1
relay18:P1
relay18:COIL1
relay19:NO2
relay19:NC2
relay19:P2
relay19:COIL2
relay19:NO1
relay19:NC1
relay19:P1
relay19:COIL1
relay20:NO2
relay20:NC2
relay20:P2
relay20:COIL2
relay20:NO1
relay20:NC1
relay20:P1
relay20:COIL1
relay21:NO2
relay21:NC2
relay21:P2
relay21:COIL2
relay21:NO1
relay21:NC1
relay21:P1
relay21:COIL1
relay22:NO2
relay22:NC2
relay22:P2
relay22:COIL2
relay22:NO1
relay22:NC1
relay22:P1
relay22:COIL1
relay23:NO2
relay23:NC2
relay23:P2
relay23:COIL2
relay23:NO1
relay23:NC1
relay23:P1
relay23:COIL1
relay24:NO2
relay24:NC2
relay24:P2
relay24:COIL2
relay24:NO1
relay24:NC1
relay24:P1
relay24:COIL1
relay25:NO2
relay25:NC2
relay25:P2
relay25:COIL2
relay25:NO1
relay25:NC1
relay25:P1
relay25:COIL1
relay26:NO2
relay26:NC2
relay26:P2
relay26:COIL2
relay26:NO1
relay26:NC1
relay26:P1
relay26:COIL1
relay27:NO2
relay27:NC2
relay27:P2
relay27:COIL2
relay27:NO1
relay27:NC1
relay27:P1
relay27:COIL1
relay28:NO2
relay28:NC2
relay28:P2
relay28:COIL2
relay28:NO1
relay28:NC1
relay28:P1
relay28:COIL1
relay29:NO2
relay29:NC2
relay29:P2
relay29:COIL2
relay29:NO1
relay29:NC1
relay29:P1
relay29:COIL1
relay30:NO2
relay30:NC2
relay30:P2
relay30:COIL2
relay30:NO1
relay30:NC1
relay30:P1
relay30:COIL1
relay31:NO2
relay31:NC2
relay31:P2
relay31:COIL2
relay31:NO1
relay31:NC1
relay31:P1
relay31:COIL1
relay32:NO2
relay32:NC2
relay32:P2
relay32:COIL2
relay32:NO1
relay32:NC1
relay32:P1
relay32:COIL1
relay33:NO2
relay33:NC2
relay33:P2
relay33:COIL2
relay33:NO1
relay33:NC1
relay33:P1
relay33:COIL1
relay34:NO2
relay34:NC2
relay34:P2
relay34:COIL2
relay34:NO1
relay34:NC1
relay34:P1
relay34:COIL1
relay35:NO2
relay35:NC2
relay35:P2
relay35:COIL2
relay35:NO1
relay35:NC1
relay35:P1
relay35:COIL1
relay36:NO2
relay36:NC2
relay36:P2
relay36:COIL2
relay36:NO1
relay36:NC1
relay36:P1
relay36:COIL1
relay37:NO2
relay37:NC2
relay37:P2
relay37:COIL2
relay37:NO1
relay37:NC1
relay37:P1
relay37:COIL1
relay38:NO2
relay38:NC2
relay38:P2
relay38:COIL2
relay38:NO1
relay38:NC1
relay38:P1
relay38:COIL1
relay39:NO2
relay39:NC2
relay39:P2
relay39:COIL2
relay39:NO1
relay39:NC1
relay39:P1
relay39:COIL1
relay40:NO2
relay40:NC2
relay40:P2
relay40:COIL2
relay40:NO1
relay40:NC1
relay40:P1
relay40:COIL1
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
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
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
btn17:1.l
btn17:2.l
btn17:1.r
btn17:2.r
btn18:1.l
btn18:2.l
btn18:1.r
btn18:2.r
btn19:1.l
btn19:2.l
btn19:1.r
btn19:2.r
btn20:1.l
btn20:2.l
btn20:1.r
btn20:2.r
btn21:1.l
btn21:2.l
btn21:1.r
btn21:2.r
btn22:1.l
btn22:2.l
btn22:1.r
btn22:2.r
btn23:1.l
btn23:2.l
btn23:1.r
btn23:2.r
btn24:1.l
btn24:2.l
btn24:1.r
btn24:2.r
btn25:1.l
btn25:2.l
btn25:1.r
btn25:2.r
btn26:1.l
btn26:2.l
btn26:1.r
btn26:2.r
btn27:1.l
btn27:2.l
btn27:1.r
btn27:2.r
btn28:1.l
btn28:2.l
btn28:1.r
btn28:2.r
btn29:1.l
btn29:2.l
btn29:1.r
btn29:2.r
btn30:1.l
btn30:2.l
btn30:1.r
btn30:2.r
btn31:1.l
btn31:2.l
btn31:1.r
btn31:2.r
btn32:1.l
btn32:2.l
btn32:1.r
btn32:2.r
btn33:1.l
btn33:2.l
btn33:1.r
btn33:2.r
btn34:1.l
btn34:2.l
btn34:1.r
btn34:2.r
btn35:1.l
btn35:2.l
btn35:1.r
btn35:2.r
btn36:1.l
btn36:2.l
btn36:1.r
btn36:2.r
btn37:1.l
btn37:2.l
btn37:1.r
btn37:2.r
btn38:1.l
btn38:2.l
btn38:1.r
btn38:2.r
btn39:1.l
btn39:2.l
btn39:1.r
btn39:2.r
btn40:1.l
btn40:2.l
btn40:1.r
btn40:2.r
74HC165
sr20:PL
sr20:CP
sr20:D4
sr20:D5
sr20:D6
sr20:D7
sr20:Q7_N
sr20:GND
sr20:Q7
sr20:DS
sr20:D0
sr20:D1
sr20:D2
sr20:D3
sr20:CE
sr20:VCC
DS: dataPin (cyan) SHCP: clockPin (violet) STCP: latchPin (limegreen)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
1 ... 2 ... 3 ... 4 ... 5 ... 6 ... 7 ... 8 ... 9 ... 10
11 . 12 .13 . 14 . 15 .16 . 17 . 18 .19 . 20
21 . 22 .23 . 24 . 25 .26 . 27 . 28 .29 . 30
31 . 32 .33 . 34 . 35 .36 . 37 . 38 .39 . 40
RELAY 1
RELAY 40
r121:1
r121:2
r122:1
r122:2
r123:1
r123:2
r124:1
r124:2
r125:1
r125:2
r126:1
r126:2
r127:1
r127:2
r128:1
r128:2
r129:1
r129:2
r130:1
r130:2
r131:1
r131:2
r132:1
r132:2
r133:1
r133:2
r134:1
r134:2
r135:1
r135:2
r136:1
r136:2
r137:1
r137:2
r138:1
r138:2
r139:1
r139:2
r140:1
r140:2
r141:1
r141:2
r142:1
r142:2
r143:1
r143:2
r144:1
r144:2
r145:1
r145:2
r146:1
r146:2
r147:1
r147:2
r148:1
r148:2
r149:1
r149:2
r150:1
r150:2
r151:1
r151:2
r152:1
r152:2
r153:1
r153:2
r154:1
r154:2
r155:1
r155:2
r156:1
r156:2
r157:1
r157:2
r158:1
r158:2
r159:1
r159:2
r160:1
r160:2