Using 6 x MCP23017 Issue

I am a relative novice to writing sketches but I have built up a PCB using a Nano and 6 x MCP23017 as pin expanders to drive Inputs from Sensors and Outputs to Bi-Colour Common Anode LED's on a Mimic Panel for a Model Railway display ...
After power up I tried to see if the correct LED's would change, so I added a simple Blink in the Setup and found that they do change but not the correct ones, so not sure what is happening...
I found there is a relationship between what is operating and what should Light ...
Also on the SCL and SDA lines I have a 4.7kohm to VCC but datasheet shows 1kohm ...
I have added part of the sketch up to the Blink test as it is quite long ...#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>
#include <Adafruit_MCP23XXX.h>
#include <wire.h>

//4 Way Crossing Mimic LED Panel system
//Created 26th April 2022
//Updated 2nd May 2022
//Completed Define and Void Setup on 3rd May 2022
//Issue on Laptop and Adafruit Bused fine
//Adding Switch Case 1st June 2022
//Added 12 Switch Case 2nd June 2022
//Initial test Mimic LED's are selecting incorrect routes but pins are correct 2nd Nov 2022

//Arduino Nano Using MCP23017 chips .. 6 in total
//Pins Used on Arduino Nano
// Nano MCP23017
// SCL SCL
// SDA SDA
// GND VSS
// 5V VDD

// MCP23017 Address
// ADDR IC21 Addr 0x22
// ADDR IC22 Addr 0x23
// ADDR IC24 Addr 0x20
// ADDR IC23 Addr 0x21
// ADDR IC1 Addr 0x24
// ADDR IC2 Addr 0x25

//define Addresses

#define addr1 0x20
#define addr2 0x21
#define addr3 0x22
#define addr4 0x23
#define addr5 0x24
#define addr6 0x25

//Mimic Panel LED's in various blocks

//On Addr 0x22 and 0x23

#define P4AB 21 //Platform 4 Exit
#define P4ENT 22 //Platform 4 Entry
#define P18A 23 //Po#define 18 Middle Track
#define P18B 24 //Po#define 18 Outside Track
#define P3ENT 25 //Platform 3 Entry
#define UP2A 26 //Up Fast Track
#define CROSS7A 27 //Scissor 7 Crossing
#define CROSS6A 28 //Scissor 6 Crossing
#define L1ABC 1 //Track 1 Left LED ABC
#define R1AB 2 //Track 1 Right LED AB
#define L2AB 3 //Track 2 Left AB
#define L2C 27 //
#define R2ABC 4 //Track 2 Right ABC
#define R2D 26
#define L3ABC 5 //Track 3 Left ABC
#define R3AB 6 //Track 3 Right AB
#define L4AB 7 //Track 4 Left AB
#define L4C 25 //
#define L4D 23 //
#define R4AB 8 //Track 4 Right AB
#define R4C 24
#define L5ABC 21 //Track 5 Left ABC
#define R5A 22 //Track 5 Right A
#define P15ABC 1 //Reversing Loop Left to Right
#define P16ABC 2 //Reversing Loop Right to Left

//Sensor inputs for Entry

#define SEN1 21//on Addr 0x21
#define SEN2 22//
#define SEN3 23//
#define SEN4 24//
#define SEN5 25//
#define SEN6 26//
#define SEN7 27//
#define SEN8 28//
#define SEN9 1//
#define SEN10 2//
#define SEN11 3//
#define SEN12 4//

int SEN1state;
int SEN2state;
int SEN3state;
int SEN4state;
int SEN5state;
int SEN6state;
int SEN7state;
int SEN8state;
int SEN9state;
int SEN10state;
int SEN11state;
int SEN12state;

//Aspect Signals Entry to blocks

#define ASP1 21// on Addr 0x20
#define ASP2 22
#define ASP3 23
#define ASP4 24
#define ASP5 25
#define ASP6 26
#define ASP7 27
#define ASP8 28

//Aspect Direction Signals

#define DIR1 1//on Addr 0x20
#define DIR2 2
#define DIR3 3
#define DIR4 4
#define DIR5 5
#define DIR6 6
#define DIR7 7
#define DIR8 8

#define DIR9 8 //on Addr 0x21

//Po#define Button Inputs
//MCP23017 Address 0x25

#define SW1 1 //Po#define Button 1
#define SW2 2 //Po#define Button 2
#define SW3 3//Po#define Button 3
#define SW4 4//Po#define Button 4
#define SW5 5 //Po#define Button 5
#define SW6 6 //Po#define Button 6
#define SW7 7 //Po#define Button 7
#define SW8 8 //Po#define Button 8

int SW1state;
int SW2state;
int SW3state;
int SW4state;
int SW5state;
int SW6state;
int SW7state;
int SW8state;

//MCP23017 Address 0x24

#define SW9 1 //Po#define Button 9
#define SW10 2 //Po#define Button 10
#define SW11 3 //Po#define Button 11
#define SW12 4//Po#define Button 12
#define SW13 5//Po#define Button 13
#define SW14 6//Po#define Button 14
#define SW15 7//Po#define Button 15
#define SW16 8//Po#define Button 16

//MCP23017 Address 0x21
#define SW17 9//Po#define Button 17
#define SW18 10//Po#define Button 18
#define SW19 11//Po#define Button 19

//MCP23017 Address 0x23
#define SW20 8//Po#define Button 20

int SW9state;
int SW10state;
int SW11state;
int SW12state;
int SW13state;
int SW14state;
int SW15state;
int SW16state;
int SW17state;
int SW18state;
int SW19state;

int TR1LEDstate;

//Po#define Button Outputs
//MCP23017 Address 0x25

#define PSW1 21//Switching Track 1 Exit Platform 3 (2 on Darcy Board)
#define PSW2 22//Switching Track 2 Fast Up Line (1 on Darcy Board)
#define PSW3 23//Switching Track 3 Exit Fast Down Line (3 on Darcy Board)
#define PSW4 24//Switching Track 4 Exit Platform 2 (4 on Darcy Board)
#define PSW5 25//Switching Track 5 Exit Platform 1 (5 on Darcy Board)
#define PSW6 26//Point6
#define PSW7 27//Point7
#define PSW8 28//Point8

//Po#define Button Outputs
//MCP23017 Address 0x24

#define PSW9 21//Point9
#define PSW10 22//Point10
#define PSW11 23//Point11
#define PSW12 24//Point12
#define PSW13 25//Point13
#define PSW14 26//Point14
#define PSW15 27//Point15
#define PSW16 28//Point16

int PSW17 = 3;//Point 17 on Nano pin 3
int PSW18 = 4;//Point 18 on Nano Pin 4
int PSW19 = 5;//Point 19 on Nano Pin 5
int PSW20 = 6;//Point 20 on Nano Pin 6

//Track Detect LED's
//MCP23017 Address 0x22

#define TR1LED 7//Track 1 Indicator
#define TR2LED 6//Track 2 Indicator
#define TR3LED 5//Track 3 Indicator
#define TR4LED 4//Track 4 Indicator
#define TR5LED 3//Track 5 Indicator

int TR6LED = 7;//Track Sense LED on Nano
int TR7LED = 8;//Reversing Loop R to L
int TR8LED = 9;//Reversing Loop L to R
int TR9LED = 10;//spare
int TR10LED = 11;//spare
int TR11LED = 12;//spare
int TR12LED = 13;//spare

//MCP23017 objects to communicate with

Adafruit_MCP23X17 mcp1;
Adafruit_MCP23X17 mcp2;
Adafruit_MCP23X17 mcp3;
Adafruit_MCP23X17 mcp4;
Adafruit_MCP23X17 mcp5;
Adafruit_MCP23X17 mcp6;

//Check which Sensor is live

bool SEN1_in_use = false;
bool SEN2_in_use = false;
bool SEN3_in_use = false;
bool SEN4_in_use = false;
bool SEN5_in_use = false;
bool SEN6_in_use = false;
bool SEN7_in_use = false;
bool SEN8_in_use = false;
bool SEN9_in_use = false;
bool SEN10_in_use = false;
bool SEN11_in_use = false;
bool SEN12_in_use = false;

// Route states
enum RouteState { Idle, Active, Clearing };

RouteState route1_state = Idle;
RouteState route2_state = Idle;
RouteState route3_state = Idle;
RouteState route4_state = Idle;
RouteState route5_state = Idle;
RouteState route6_state = Idle;
RouteState route1A_state = Idle;
RouteState route2A_state = Idle;
RouteState route3A_state = Idle;
RouteState route4A_state = Idle;
RouteState route5A_state = Idle;
RouteState route6A_state = Idle;

void setup() {
Serial.begin(9600);
Serial.println(" 4 Way crossing Control using 6 x MCP23017 ");

//init devices
if (!mcp1.begin_I2C(addr1)){
Serial.println("Cant initialise mcp23017 mcp1");
while (1);
}
if (!mcp2.begin_I2C(addr2)){
Serial.println("Cant initialise mcp23017 mcp2");
while (1);
}
if (!mcp3.begin_I2C(addr3)){
Serial.println("Cant initialise mcp23017 mcp3");
while (1);
}
if (!mcp4.begin_I2C(addr4)){
Serial.println("Cant initialise mcp23017 mcp4");
while (1);
}
if (!mcp5.begin_I2C(addr5)){
Serial.println("Cant initialise mcp23017 mcp5");
while (1);
}
if (!mcp6.begin_I2C(addr6)){
Serial.println("Cant initialise mcp23017 mcp6");
while (1);
}
Serial.println("All MCP's are working");

//Mimic LED Blocks setup Addr 0x22 and 0x23 mcp 3 & mcp4
//Also Blinks on second set of LED's as per List
mcp3.pinMode(P4AB,OUTPUT);// Pin 21 but blinking on R3AB on Pin 6
mcp3.pinMode(P4ENT,OUTPUT);// Pin 22 but Blinking on L4AB 0n Pin 7
mcp3.pinMode(P18A,OUTPUT);// Pin 23 but Blinking on R4AB on pin 8
mcp3.pinMode(P18B,OUTPUT);// Pin 24 but Blinking on L1ABC on Pin 1 XXX
mcp3.pinMode(P3ENT,OUTPUT);// Pin 25 but Blinking on R1AB Pin 2
mcp3.pinMode(UP2A,OUTPUT);// Pin 26 but Blinking on L2AB Pin 3
mcp3.pinMode(CROSS7A,OUTPUT);// Pin 27 but Blinking on R2ABC Pin 4
mcp3.pinMode(CROSS6A,OUTPUT);// Pin 28 but Blinking on L3ABC Pin 5
mcp3.pinMode(L1ABC,OUTPUT);// Pin 1 but Blinking on P4ENT Pin 22
mcp3.pinMode(R1AB,OUTPUT);// Pin 2 but Blinking on P18A Pin 23
mcp3.pinMode(L2AB,OUTPUT);// Pin 3 but Blinking on P18B Pin 24
mcp4.pinMode(L2C,OUTPUT);// Pin 27 but Blinking on TRLED4 Pin 6
mcp3.pinMode(R2ABC,OUTPUT);// Pin 4 but Blinking on P3ENT Pin 25
mcp4.pinMode(R2D,OUTPUT);// Pin 26 but blinking on TRLED5 on Pin 3
mcp3.pinMode(L3ABC,OUTPUT);// Pin 5 but Blinking on UP2A on Pin 26
mcp3.pinMode(R3AB,OUTPUT);// Pin 6 but Blinking on CROSS7A on Pin 27
mcp3.pinMode(L4AB,OUTPUT);// Pin 7 but Blinking on CROSS6A on Pin 28
mcp4.pinMode(L4C,OUTPUT);// Pin 25 but nothing Blinking
mcp4.pinMode(L4D,OUTPUT);// Pin 23 but Blinking on R4AB on Pin 8
mcp3.pinMode(R4AB,OUTPUT);// Pin 8 but Blinking on L1ABC on Pin 1 XXX
mcp4.pinMode(R4C,OUTPUT);// Pin 24 but blinking on P15ABC on Pin 1
mcp4.pinMode(L5ABC,OUTPUT);// Pin 21 but blinking on TR2LED on Pin 6
mcp4.pinMode(R5A,OUTPUT);// Pin 22 but blinking on TR1LED on Pin 7
mcp4.pinMode(P15ABC,OUTPUT);// Pin 1 But blinking on R5A on Pin 22
mcp4.pinMode(P16ABC,OUTPUT);// Pin 2 but blniking on L4D on Pin 23

mcp3.digitalWrite(R4AB,LOW);
delay(1000);
Serial.print ("0x");
Serial.println (addr4,HEX);
Serial.print ("Pin ");
Serial.println (R4AB);

mcp3.digitalWrite(R4AB,HIGH);
delay(1000);
mcp3.digitalWrite(R4AB,LOW);
delay(1000);
mcp3.digitalWrite(R4AB,HIGH);
delay(1000);
mcp3.digitalWrite(R4AB,LOW);
delay(1000);
mcp3.digitalWrite(R4AB,HIGH);
delay(1000);
mcp3.digitalWrite(R4AB,LOW);
delay(1000);
mcp3.digitalWrite(R4AB,HIGH);
delay(1000);

Read the forum guidelines to see how to properly post code and some good information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

Neither are we. We cannot see what is happening, you will have to tell us, or show us.

Did you write a shorter bit of code to see if you could control just the MCP23017s by themselves?

I cant really show you but the Pins that are being activated are towards the end of the sketch and are comments on each Output Line


with a description on each line as to the pins that are being activated...
i.e mcp3.pinMode(P4AB,OUTPUT);// Pin 21 but blinking on R3AB on Pin 6
I could upload a screenshot of the schematic but it will probably look small ...
My next step is to try the Resistors on SCL and SDA line then look at the SCL and SCA on a scope ...

Look at the documentation for the Adafruit MCP23017 library. You don't use the actual pin numbers. You use the "Pin ID" value

For example, your P4AB is 21 but it should be 0

MCP23x17 
Pin #	     Pin Name	Pin ID
21				GPA0	0
22				GPA1	1
23				GPA2	2
24				GPA3	3
25				GPA4	4
26				GPA5	5
27				GPA6	6
28				GPA7	7
1				GPB0	8
2				GPB1	9
3				GPB2	10
4				GPB3	11
5				GPB4	12
6				GPB5	13
7				GPB6	14
8				GPB7	15

You could also save yourself a ton of repetition and work if you use arrays

Sorry but comments I get back dont really help, My main hobby is Model Railways and Arduino's are a part which helps a lot, My main line of work is or Was Electronics most of my working Life and now in my mid 70's I get into Model Railways 3 Years ago and Arduino's so programming is not easy at my age...
I have had a lot of help in the past and particularly from Model Electronic Railway Group ... MERG ... but no one seems to have used MCP23017 as no help, I have also done a similar sketch using a Mega and using all but 2 of the Data and Analog pins to control another section of my layout .. this sketch had 12 Switch Case and got some start from a member of MERG ... and this works great ...
This project is for another section of my layout which is controlled via toggle switches ... there is no great rush to finish as I wont need till next spring when I can run Trains when its not freezing ...

Main points...
1... I dont use Arrays as it creates more complex, I can easily read what I am doing the way I construct the sketch...
2... I built up the Idea of using MCP23017's after watching numberous Youtube channels and used one channel which helped in the start of the sketch ... this channel used 2 MCp23017's and I breadboarded and it worked well ...
3... I could have used PCF8574 devices which I have used in the past ... sketch on MERG-DEV · GitHub or PCA9685 which I have used for Servo's but the MCP23017 looked just the ticket for the near 100 Input/Outputs I required ...
4... The PCB was constructed with parts I have used quite succesfully in the past i.e CD4069/4049 and ULN2803 plus various other logic devices from Aspect Signal projects so to add an I2C bus seem the way to go ... the PCB from JLCPCB cost £5 each so not beaking the bank...
5 ... As there is a pattern to the defined pins and the actual pins which seems to be an offset of 23 and 15 except for 2 pins which trigger the same set of LED's, I could re-define the pins to the pin that works for that set of LED's but its a bodge and not correct to schematic ...

This triggered me. 23 and 15 are actually 24 and 16, if one fails to account for bits being numbered from 0 instead of 1; you haven't perchance simply made an off-by-1 error somewhere in your bitcounting, have you?

I noticed that you don't have any supply decoupling on any of the chips. Each expander chip needs a 0.1uF ceramic between power and ground as close to the pins as you can get them. Try this by soldering the capacitors on the back of the board.

You schematic is far too small to see. Try reposting it with much more resolution.

No I didnt have any De-coupling on the MCP23017 so I added 0.1mfd this afternoon onto the MCP23017 pins also on the Nano, but made no difference, I even tried another New Nano and a Uno with wire links and it was just the same, I did do a Bodge with the Defined Pins by adding or deducting the amount of the Error and seemed to work but I did this way so I know what pins are connected to what bank of LED's ..
I cant make the Schematic any clearer as its just a screen shot from Eagle Cad, I could put the Eagle file up but depends if you have Eagle Cad ...
Its very strange as there is a standard error on pins as you can see from the #Defined Pins ...
I could look at creating an Array but I think I would get lost in doing it thats why I did this way ...
John

The #defined numbers are the MCP23017 pin numbers as per schematic, but I found there seem to be a common offset as per my recent post to Grumpy Mike ... I am going to breadboard the original sketch I found on Youtube and see what happens ...
This was the links ...

So were these ceramic capacitors? They have to be ceramic.

Thing is that is probably not the only problem, but these are essential when using external components.

I am sure that you can export a PDF or some other stuff from your PCB CAD, because it looks like your order problem is actually down to using the wrong pins, but I can't make out what your wiring is so I can't check.

And posting an image of your code is little more than useless. Posting the code correctly will allow us to load it an and see what it is doing. As it is your code is very very hard to read basically because of the repeated copy and paste.

Hi Grumpy Mike, I have saved the image as a PNG file so should be able to veiw it, I did post the full sketch but not sure where it went so will try again ...
I did make a few mods to sketch and as posts above bodged the #Define pins to see if that works, BUT I think the problem may be in the Libraries as ther are these : ==

#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>
#include <Adafruit_MCP23XXX.h>
#include <wire.h>
So I am just about to just include the Adafruit MCP23X17.h and see if that is the problem ...

Full sketch below ...



#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>
#include <Adafruit_MCP23XXX.h>
#include <wire.h>


 //4 Way Crossing Mimic LED Panel system
 //Created 26th April 2022
 //Updated 2nd May 2022
 //Completed Define and Void Setup on 3rd May 2022
 //Issue on Laptop and Adafruit Bused fine
 //Adding Switch Case 1st June 2022
//Added 12 Switch Case 2nd June 2022
//Initial test Mimic LED's are selecting incorrect routes but pins are correct 2nd Nov 2022

 //Arduino Nano Using MCP23017 chips .. 6 in total
 //Pins Used on Arduino Nano
 //  Nano          MCP23017
 //   SCL            SCL
 //   SDA            SDA
 //   GND            VSS
 //    5V            VDD

 // MCP23017 Address
 //  ADDR IC21   Addr 0x22
 //  ADDR IC22   Addr 0x23
 //  ADDR IC24   Addr 0x20
 //  ADDR IC23   Addr 0x21
 //  ADDR IC1    Addr 0x24
 //  ADDR IC2    Addr 0x25


//define Addresses

#define addr1 0x20
#define addr2 0x21
#define addr3 0x22
#define addr4 0x23
#define addr5 0x24
#define addr6 0x25
 
//Mimic Panel LED's in various blocks

//On Addr 0x22 and 0x23

 #define P4AB   21 //Platform 4 Exit
 #define P4ENT  22 //Platform 4 Entry
 #define P18A   23 //Po#define 18 Middle Track
 #define P18B   24 //Po#define 18 Outside Track
 #define P3ENT  25 //Platform 3 Entry
 #define UP2A   26 //Up Fast Track
 #define CROSS7A  27 //Scissor 7 Crossing
 #define CROSS6A  28 //Scissor 6 Crossing
 #define L1ABC  1 //Track 1 Left LED ABC
 #define R1AB   2 //Track 1 Right LED AB
 #define L2AB   3 //Track 2 Left AB
 #define L2C    27 //
 #define R2ABC  4 //Track 2 Right ABC
 #define R2D    26
 #define L3ABC  5 //Track 3 Left ABC
 #define R3AB   6 //Track 3 Right AB
 #define L4AB   7 //Track 4 Left AB
 #define L4C    25 //
 #define L4D    23 //
 #define R4AB   8 //Track 4 Right AB
 #define R4C    24
 #define L5ABC  21 //Track 5 Left ABC
 #define R5A    22 //Track 5 Right A
 #define P15ABC 1 //Reversing Loop Left to Right
 #define P16ABC 2 //Reversing Loop Right to Left
 
 

//Sensor inputs for Entry

  #define SEN1  21//on Addr 0x21
  #define SEN2  22// 
  #define SEN3  23// 
  #define SEN4  24// 
  #define SEN5  25// 
  #define SEN6  26//
  #define SEN7  27//
  #define SEN8  28//
  #define SEN9  1//
  #define SEN10  2//
  #define SEN11  3//
  #define SEN12  4//

  int SEN1state;
  int SEN2state;
  int SEN3state;
  int SEN4state;
  int SEN5state;
  int SEN6state;
  int SEN7state;
  int SEN8state;
  int SEN9state;
  int SEN10state;
  int SEN11state;
  int SEN12state;

//Aspect Signals Entry to blocks

  #define ASP1  21// on Addr 0x20
  #define ASP2  22
  #define ASP3  23
  #define ASP4  24
  #define ASP5  25
  #define ASP6  26
  #define ASP7  27
  #define ASP8  28

//Aspect Direction Signals

  #define DIR1  1//on Addr 0x20
  #define DIR2  2
  #define DIR3  3
  #define DIR4  4
  #define DIR5  5
  #define DIR6  6
  #define DIR7  7
  #define DIR8  8
  
  #define DIR9  8 //on Addr 0x21

//Po#define Button Inputs
//MCP23017 Address 0x25

  #define SW1  1 //Po#define Button 1
  #define SW2  2 //Po#define Button 2
  #define SW3  3//Po#define Button 3
  #define SW4  4//Po#define Button 4
  #define SW5  5 //Po#define Button 5
  #define SW6  6 //Po#define Button 6
  #define SW7  7 //Po#define Button 7
  #define SW8  8 //Po#define Button 8

  int SW1state;
  int SW2state;
  int SW3state;
  int SW4state;
  int SW5state;
  int SW6state;
  int SW7state;
  int SW8state;

//MCP23017 Address 0x24

  #define SW9  1 //Po#define Button 9
  #define SW10  2 //Po#define Button 10
  #define SW11  3 //Po#define Button 11
  #define SW12  4//Po#define Button 12
  #define SW13  5//Po#define Button 13
  #define SW14  6//Po#define Button 14
  #define SW15  7//Po#define Button 15
  #define SW16  8//Po#define Button 16

//MCP23017 Address 0x21
  #define SW17  9//Po#define Button 17
  #define SW18  10//Po#define Button 18
  #define SW19  11//Po#define Button 19

//MCP23017 Address 0x23
  #define SW20  8//Po#define Button 20

  int SW9state;
  int SW10state;
  int SW11state;
  int SW12state;
  int SW13state;
  int SW14state;
  int SW15state;
  int SW16state;
  int SW17state;
  int SW18state;
  int SW19state;

  int TR1LEDstate;



//Po#define Button Outputs
//MCP23017 Address 0x25

  #define PSW1  21//Switching Track 1 Exit Platform 3 (2 on Darcy Board)
  #define PSW2  22//Switching Track 2 Fast Up Line (1 on Darcy Board)
  #define PSW3  23//Switching Track 3 Exit Fast Down Line (3 on Darcy Board)
  #define PSW4  24//Switching Track 4 Exit Platform 2 (4 on Darcy Board)
  #define PSW5  25//Switching Track 5 Exit Platform 1 (5 on Darcy Board)
  #define PSW6  26//Point6
  #define PSW7  27//Point7
  #define PSW8  28//Point8

  //Po#define Button Outputs
//MCP23017 Address 0x24

  #define PSW9  21//Point9 
  #define PSW10  22//Point10
  #define PSW11  23//Point11 
  #define PSW12  24//Point12 
  #define PSW13  25//Point13 
  #define PSW14  26//Point14
  #define PSW15  27//Point15
  #define PSW16  28//Point16

  int PSW17 = 3;//Point 17 on Nano pin 3
  int PSW18 = 4;//Point 18 on Nano Pin 4
  int PSW19 = 5;//Point 19 on Nano Pin 5
  int PSW20 = 6;//Point 20 on Nano Pin 6

//Track Detect LED's
//MCP23017 Address 0x22

  #define TR1LED  7//Track 1 Indicator
  #define TR2LED  6//Track 2 Indicator
  #define TR3LED  5//Track 3 Indicator
  #define TR4LED  4//Track 4 Indicator
  #define TR5LED  3//Track 5 Indicator

  int TR6LED = 7;//Track Sense LED on Nano
  int TR7LED = 8;//Reversing Loop R to L
  int TR8LED = 9;//Reversing Loop L to R
  int TR9LED = 10;//spare
  int TR10LED = 11;//spare
  int TR11LED = 12;//spare
  int TR12LED = 13;//spare

  

  

//MCP23017 objects to communicate with

Adafruit_MCP23X17 mcp1;
Adafruit_MCP23X17 mcp2;
Adafruit_MCP23X17 mcp3;
Adafruit_MCP23X17 mcp4;
Adafruit_MCP23X17 mcp5;
Adafruit_MCP23X17 mcp6;

//Check which Sensor is live

  bool SEN1_in_use = false;
  bool SEN2_in_use = false;
  bool SEN3_in_use = false;
  bool SEN4_in_use = false;
  bool SEN5_in_use = false;
  bool SEN6_in_use = false;
  bool SEN7_in_use = false;
  bool SEN8_in_use = false;
  bool SEN9_in_use = false;
  bool SEN10_in_use = false;
  bool SEN11_in_use = false;
  bool SEN12_in_use = false;

// Route states
enum RouteState { Idle, Active, Clearing };

RouteState  route1_state = Idle;
RouteState  route2_state = Idle;
RouteState  route3_state = Idle;
RouteState  route4_state = Idle;
RouteState  route5_state = Idle;
RouteState  route6_state = Idle;
RouteState  route1A_state = Idle;
RouteState  route2A_state = Idle;
RouteState  route3A_state = Idle;
RouteState  route4A_state = Idle;
RouteState  route5A_state = Idle;
RouteState  route6A_state = Idle;

 
  
void setup() {
  Serial.begin(9600);
  Serial.println(" 4 Way crossing Control using 6 x MCP23017 ");

//init devices
  if (!mcp1.begin_I2C(addr1)){
    Serial.println("Cant initialise mcp23017 mcp1");
    while (1);
  }
  if (!mcp2.begin_I2C(addr2)){
    Serial.println("Cant initialise mcp23017 mcp2");
    while (1);
  }
  if (!mcp3.begin_I2C(addr3)){
    Serial.println("Cant initialise mcp23017 mcp3");
    while (1);
  }
  if (!mcp4.begin_I2C(addr4)){
    Serial.println("Cant initialise mcp23017 mcp4");
    while (1);
  }
  if (!mcp5.begin_I2C(addr5)){
    Serial.println("Cant initialise mcp23017 mcp5");
    while (1);
  }
  if (!mcp6.begin_I2C(addr6)){
    Serial.println("Cant initialise mcp23017 mcp6");
    while (1);
  }
    Serial.println("All MCP's are working");

//Mimic LED Blocks setup Addr 0x22 and 0x23 mcp 3 & mcp4 
//Also Blinks on second set of LED's as per List  
   mcp3.pinMode(P4AB,OUTPUT);//    Pin 21 but blinking on R3AB on Pin 6
   mcp3.pinMode(P4ENT,OUTPUT);//   Pin 22 but Blinking on L4AB 0n Pin 7
   mcp3.pinMode(P18A,OUTPUT);//    Pin 23 but Blinking on R4AB on pin 8
   mcp3.pinMode(P18B,OUTPUT);//    Pin 24 but Blinking on L1ABC on Pin 1 XXX
   mcp3.pinMode(P3ENT,OUTPUT);//   Pin 25 but Blinking on R1AB Pin 2
   mcp3.pinMode(UP2A,OUTPUT);//    Pin 26 but Blinking on L2AB Pin 3
   mcp3.pinMode(CROSS7A,OUTPUT);// Pin 27 but Blinking on R2ABC Pin 4
   mcp3.pinMode(CROSS6A,OUTPUT);// Pin 28 but Blinking on L3ABC Pin 5
   mcp3.pinMode(L1ABC,OUTPUT);//   Pin 1 but Blinking on P4ENT Pin 22
   mcp3.pinMode(R1AB,OUTPUT);//    Pin 2 but Blinking on P18A Pin 23
   mcp3.pinMode(L2AB,OUTPUT);//    Pin 3 but Blinking on P18B Pin 24
   mcp3.pinMode(R2ABC,OUTPUT);//   Pin 4 but Blinking on P3ENT Pin 25
   mcp3.pinMode(L3ABC,OUTPUT);//   Pin 5 but Blinking on UP2A on Pin 26
   mcp3.pinMode(R3AB,OUTPUT);//    Pin 6 but Blinking on CROSS7A on Pin 27
   mcp3.pinMode(L4AB,OUTPUT);//    Pin 7 but Blinking on CROSS6A on Pin 28
   mcp3.pinMode(R4AB,OUTPUT);//    Pin 8 but Blinking on L1ABC on Pin 1 XXX
   

   mcp4.pinMode(R2D,OUTPUT);//     Pin 26 but blinking on TRLED5 on Pin 3
   mcp4.pinMode(L2C,OUTPUT);//     Pin 27 but Blinking on TRLED4 Pin 6    
   mcp4.pinMode(L4C,OUTPUT);//     Pin 25 but nothing Blinking
   mcp4.pinMode(L4D,OUTPUT);//     Pin 23 but Blinking on R4AB on Pin 8   
   mcp4.pinMode(R4C,OUTPUT);//     Pin 24 but blinking on P15ABC on Pin 1
   mcp4.pinMode(L5ABC,OUTPUT);//   Pin 21 but blinking on TR2LED on Pin 6 
   mcp4.pinMode(R5A,OUTPUT);//     Pin 22 but blinking on TR1LED on Pin 7
   mcp4.pinMode(P15ABC,OUTPUT);//  Pin 1 But blinking on R5A on Pin 22
   mcp4.pinMode(P16ABC,OUTPUT);//  Pin 2 but blniking on L4D on Pin 23 

   
//A Test blinking of LED banks
   mcp3.digitalWrite(R4AB,LOW);
    delay(1000); 
   mcp3.digitalWrite(R4AB,HIGH);
   delay(1000);
   mcp3.digitalWrite(R4AB,LOW);
    delay(1000); 
   mcp3.digitalWrite(R4AB,HIGH);
   delay(1000);
   mcp3.digitalWrite(R4AB,LOW);
    delay(1000);
   mcp3.digitalWrite(R4AB,HIGH);
   delay(1000);
   mcp3.digitalWrite(R4AB,LOW);
    delay(1000); 
   mcp3.digitalWrite(R4AB,HIGH);
   delay(1000);

    Serial.print ("0x");
    Serial.println (addr4,HEX);
    Serial.print ("Pin "); 
    Serial.println (R4AB);   
   
   

   
//Entry Sensors setup Addr 0x21 mcp2

   mcp2.pinMode(SEN1,INPUT_PULLUP);
   mcp2.pinMode(SEN2,INPUT_PULLUP);
   mcp2.pinMode(SEN3,INPUT_PULLUP);
   mcp2.pinMode(SEN4,INPUT_PULLUP);
   mcp2.pinMode(SEN5,INPUT_PULLUP);   
   mcp2.pinMode(SEN6,INPUT_PULLUP);
   mcp2.pinMode(SEN7,INPUT_PULLUP);
   mcp2.pinMode(SEN8,INPUT_PULLUP);
   mcp2.pinMode(SEN9,INPUT_PULLUP);
   mcp2.pinMode(SEN10,INPUT_PULLUP);
   mcp2.pinMode(SEN11,INPUT_PULLUP);
   mcp2.pinMode(SEN12,INPUT_PULLUP);

//Aspect Signal Setup Addr 0x20 mcp1 and DIR9 is Addr ox21 mcp2

  mcp1.pinMode(ASP1,OUTPUT);
  mcp1.pinMode(ASP2,OUTPUT);
  mcp1.pinMode(ASP3,OUTPUT);
  mcp1.pinMode(ASP4,OUTPUT);
  mcp1.pinMode(ASP5,OUTPUT);
  mcp1.pinMode(ASP6,OUTPUT);
  mcp1.pinMode(ASP7,OUTPUT);
  mcp1.pinMode(ASP8,OUTPUT);

  
  mcp1.pinMode(DIR1,OUTPUT);
  mcp1.pinMode(DIR2,OUTPUT);
  mcp1.pinMode(DIR3,OUTPUT);
  mcp1.pinMode(DIR4,OUTPUT);
  mcp1.pinMode(DIR5,OUTPUT);
  mcp1.pinMode(DIR6,OUTPUT);
  mcp1.pinMode(DIR7,OUTPUT);
  mcp1.pinMode(DIR8,OUTPUT);
  mcp2.pinMode(DIR9,OUTPUT);

//Po#define Button Inputs Button1 - Button8 SW1 - SW8 Addr 0x25 mcp6 .. Button9 - Button16 SW9 - SW16 Addr 0x24 mcp5
//Buttons17 - Button19 SW17 - SW19 Addr 0x21 mcp2 

  mcp6.pinMode(SW1,INPUT_PULLUP);
  mcp6.pinMode(SW2,INPUT_PULLUP);
  mcp6.pinMode(SW3,INPUT_PULLUP);
  mcp6.pinMode(SW4,INPUT_PULLUP);
  mcp6.pinMode(SW5,INPUT_PULLUP);
  mcp6.pinMode(SW6,INPUT_PULLUP);
  mcp6.pinMode(SW7,INPUT_PULLUP);
  mcp6.pinMode(SW8,INPUT_PULLUP);

  mcp5.pinMode(SW9,INPUT_PULLUP);
  mcp5.pinMode(SW10,INPUT_PULLUP);
  mcp5.pinMode(SW11,INPUT_PULLUP);
  mcp5.pinMode(SW13,INPUT_PULLUP);
  mcp5.pinMode(SW14,INPUT_PULLUP);
  mcp5.pinMode(SW15,INPUT_PULLUP);
  mcp5.pinMode(SW16,INPUT_PULLUP);
  
  mcp2.pinMode(SW17,INPUT_PULLUP);
  mcp2.pinMode(SW18,INPUT_PULLUP);
  mcp2.pinMode(SW19,INPUT_PULLUP);

  mcp4.pinMode(SW20,INPUT_PULLUP);

//Po#define Switching 1 - 5 are "Y" po#defines 
//Po#define Switch Outputs on Addr 0x25 PSW1 - PSW8

  mcp6.pinMode(PSW1,OUTPUT);
  mcp6.pinMode(PSW2,OUTPUT);
  mcp6.pinMode(PSW3,OUTPUT);
  mcp6.pinMode(PSW4,OUTPUT);
  mcp6.pinMode(PSW5,OUTPUT);
  mcp6.pinMode(PSW6,OUTPUT);
  mcp6.pinMode(PSW7,OUTPUT);
  mcp6.pinMode(PSW8,OUTPUT);

//define Switching 9 - 16 
//define Switch Outputs on Addr 0x24 PSW9 - PSW16

  mcp5.pinMode(PSW9,OUTPUT);
  mcp5.pinMode(PSW10,OUTPUT);
  mcp5.pinMode(PSW11,OUTPUT);
  mcp5.pinMode(PSW12,OUTPUT);
  mcp5.pinMode(PSW13,OUTPUT);
  mcp5.pinMode(PSW14,OUTPUT);
  mcp5.pinMode(PSW15,OUTPUT);
  mcp5.pinMode(PSW16,OUTPUT);
  
//int Switch Point Outputs on Nano Pins 3,4,5,6 PSW17 - PSW20
   pinMode(PSW17,OUTPUT);
   pinMode(PSW18,OUTPUT);
   pinMode(PSW19,OUTPUT);
   pinMode(PSW20,OUTPUT);

//Sensor LED's for Tracks 1,2 and top of Layout Entry 3.4.5
//Track Detect LED's Addr 0x23 mcp4

  mcp4.pinMode(TR1LED,OUTPUT);//Track 1 Indicator
  mcp4.pinMode(TR2LED,OUTPUT);//Track 2 Indicator
  mcp4.pinMode(TR3LED,OUTPUT);//Track 3 Indicator
  mcp4.pinMode(TR4LED,OUTPUT);//Track 4 Indicator
  mcp4.pinMode(TR5LED,OUTPUT);//Track 5 Indicator
       pinMode(TR6LED,OUTPUT);//Track 6 Indicator plus 7 to 12 from Nano
       pinMode(TR7LED,OUTPUT);//Reversing Loop R to L
       pinMode(TR8LED,OUTPUT);//Reversing Loop L to R
       pinMode(TR9LED,OUTPUT);//spare
       pinMode(TR10LED,OUTPUT);//spare
       pinMode(TR11LED,OUTPUT);//spare
       pinMode(TR12LED,OUTPUT);//spare
}

void loop(){
  
  SEN1state = mcp2.digitalRead(SEN1);

// ...... Route 5 from Sensor 1 ot Platform 1 .......

  switch (route5_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN1) == LOW && mcp6.digitalRead(SW5) == HIGH) {
      
        mcp4.digitalWrite(TR5LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN1) == LOW && mcp6.digitalRead(SW5) == LOW) {
      
        if (mcp6.digitalRead(SW5) == LOW && !SEN1_in_use) {
          
        SEN1_in_use = true;

        mcp6.digitalWrite(PSW5, LOW);//Point switch to Right
        mcp1.digitalWrite(ASP1, HIGH);//Aspect on Sen1 Green
        mcp1.digitalWrite(DIR1, LOW);//Dierection OFF
        mcp5.digitalWrite(PSW15, HIGH);//Point 15 Straight
        mcp5.digitalWrite(PSW13, LOW);//Point 13 Right
             digitalWrite(PSW19, HIGH);//Point 19 Straight (Nano Switch)4
        mcp4.digitalWrite(R4C, HIGH);//Mimic R4C GREEN
        mcp4.digitalWrite(R5A, HIGH);//Mimic R5A GREEN
   
          route5_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN1) == HIGH) { 
                      
        route5_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN1) == HIGH) {

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to Left
        mcp1.digitalWrite(ASP1, LOW);//Aspect on Sen1 RED
        mcp1.digitalWrite(DIR1, LOW);//Dierection OFF
        mcp5.digitalWrite(PSW15, HIGH);//Point 15 Straight
        mcp5.digitalWrite(PSW13, HIGH);//Point 13 Straight
             digitalWrite(PSW19, HIGH);//Point 19 Straight (Nano Switch)4
        mcp4.digitalWrite(R4C, LOW);//Mimic R4C RED
        mcp4.digitalWrite(R5A, LOW);//Mimic R5A RED

        SEN1_in_use = false;
        route5_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 5A from Sensor 2 to Platform 1 .......

  switch (route5A_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN2) == LOW && mcp6.digitalRead(SW5) == HIGH) {
      
        mcp4.digitalWrite(TR5LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN2) == LOW && mcp6.digitalRead(SW5) == LOW) {
      
        if (mcp6.digitalRead(SW5) == LOW && !SEN2_in_use) {
          
        SEN2_in_use = true;

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to Left
        mcp1.digitalWrite(ASP3, HIGH);//Aspect on Sen2 Green
        mcp1.digitalWrite(ASP4, HIGH);//Aspect on Track GREEN
        mcp1.digitalWrite(DIR5, HIGH);//Direction LEFT
        mcp5.digitalWrite(PSW11, HIGH);//Point 11 Straight
        mcp5.digitalWrite(PSW8, LOW);//Point Left
        mcp4.digitalWrite(L4D, HIGH);//Mimic L4D GREEN
        mcp4.digitalWrite(L4C, HIGH);//Mimic L4C GREEN
        mcp4.digitalWrite(L5ABC, HIGH);//Mimic L5ABC GREEN     
   
          route5A_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN2) == HIGH) { 
                      
        route5A_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN2) == HIGH) {

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP3, LOW);//Aspect on Sen2 RED
        mcp1.digitalWrite(ASP4, LOW);//Aspect on Track RED
        mcp1.digitalWrite(DIR5, LOW);//Direction OFF
        mcp5.digitalWrite(PSW11, HIGH);//Point 11 STRAIGHT
        mcp5.digitalWrite(PSW8, HIGH);//Point STRAIGHT
        mcp4.digitalWrite(L4D, LOW);//Mimic L4D RED
        mcp4.digitalWrite(L4C, LOW);//Mimic L4C RED
        mcp4.digitalWrite(L5ABC, LOW);//Mimic L5ABC RED

         SEN2_in_use = false;
        route5A_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 4 from Sensor 1 ot Platform 2 .......

  switch (route4_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN1) == LOW && mcp6.digitalRead(SW4) == HIGH) {
      
        mcp4.digitalWrite(TR4LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN1) == LOW && mcp6.digitalRead(SW4) == LOW) {
      
        if (mcp6.digitalRead(SW4) == LOW && !SEN1_in_use) {
          
        SEN1_in_use = true;

        mcp6.digitalWrite(PSW4, LOW);//Point switch to RIGHT
        mcp1.digitalWrite(ASP1, HIGH);//Aspect on Sen1 Green
        mcp1.digitalWrite(DIR1, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR3, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR2, LOW);//Direction LEFT
        mcp5.digitalWrite(PSW15, LOW);//Point 15 STRAIGHT
        mcp5.digitalWrite(PSW13, LOW);//Point 15 STRAIGHT
        mcp5.digitalWrite(PSW12, HIGH);//Point 15 LEFT                     
        mcp4.digitalWrite(R4C, HIGH);//Mimic R4C GREEN
        mcp4.digitalWrite(R4AB, HIGH);//Mimic R4AB GREEN
   
          route4_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN1) == HIGH) { 
                      
        route4_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN1) == HIGH) {
     mcp5.digitalWrite(PSW12, LOW);//Point 12 STRAIGHT                     
        mcp4.digitalWrite(R4C, LOW);//Mimic R4C RED
        mcp4.digitalWrite(R4AB, LOW);//Mimic R4AB RED

        SEN1_in_use = false;
        route4_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 4A from Sensor 2 to Platform 2 .......

  switch (route4A_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN2) == LOW && mcp6.digitalRead(SW4) == HIGH) {
      
        mcp4.digitalWrite(TR4LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN2) == LOW && mcp6.digitalRead(SW4) == LOW) {
      
        if (mcp6.digitalRead(SW4) == LOW && !SEN2_in_use) {
          
        SEN2_in_use = true;

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP3, HIGH);//Aspect on Sen2 GREEN
        mcp1.digitalWrite(ASP4, HIGH);//Aspect on Track GREEN
        mcp1.digitalWrite(DIR5, LOW);//Direction OFF
        mcp5.digitalWrite(PSW11, HIGH);//Point 11 STRAIGHT
        mcp5.digitalWrite(PSW8, HIGH);//Point STRAIGHT
        mcp4.digitalWrite(L4D, HIGH);//Mimic L4D GREEN
        mcp4.digitalWrite(L4C, HIGH);//Mimic L4C GREEN
        mcp3.digitalWrite(L4AB, HIGH);//Mimic L4AB GREEN
             
   
          route4A_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN2) == HIGH) { 
                      
        route4A_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN2) == HIGH) {

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP3, LOW);//Aspect on Sen2 GREEN
        mcp1.digitalWrite(ASP4, LOW);//Aspect on Track GREEN
        mcp1.digitalWrite(DIR5, LOW);//Direction OFF
        mcp5.digitalWrite(PSW11, HIGH);//Point 11 STRAIGHT
        mcp5.digitalWrite(PSW8, HIGH);//Point STRAIGHT
        mcp4.digitalWrite(L4D, LOW);//Mimic L4D RED
        mcp4.digitalWrite(L4C, LOW);//Mimic L4C RED
        mcp3.digitalWrite(L4AB, LOW);//Mimic L4AB RED

         SEN2_in_use = false;
        route4A_state = Idle;
        }
     }
      break;
    }
// ...... Route 3 from Sensor 1 ot Fast Down Line .......

  switch (route3_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN1) == LOW && mcp6.digitalRead(SW3) == HIGH) {
      
        mcp4.digitalWrite(TR3LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN1) == LOW && mcp6.digitalRead(SW3) == LOW) {
      
        if (mcp6.digitalRead(SW3) == LOW && !SEN1_in_use) {
          
        SEN1_in_use = true;

        mcp6.digitalWrite(PSW3, LOW);//Point switch to RIGHT
        mcp1.digitalWrite(ASP1, HIGH);//Aspect on Sen1 Green
        mcp1.digitalWrite(DIR1, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR3, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR2, HIGH);//Direction OFF
        mcp5.digitalWrite(PSW15, LOW);//Point 15 STRAIGHT
        mcp5.digitalWrite(PSW13, LOW);//Point 13 STRAIGHT
        mcp5.digitalWrite(PSW12, LOW);//Point 12 STRAIGHT                     
        mcp4.digitalWrite(R4C, HIGH);//Mimic R4C GREEN
        mcp3.digitalWrite(R3AB, HIGH);//Mimic R3AB GREEN
   
          route3_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN1) == HIGH) { 
                      
        route3_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN1) == HIGH) {
        
        mcp6.digitalWrite(PSW3, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP1, LOW);//Aspect on Sen1 RED
        mcp1.digitalWrite(DIR1, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR3, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR2, HIGH);//Direction OFF
        mcp5.digitalWrite(PSW15, LOW);//Point 15 STRAIGHT
        mcp5.digitalWrite(PSW13, LOW);//Point 13 STRAIGHT
        mcp5.digitalWrite(PSW12, LOW);//Point 12 STRAIGHT                     
        mcp4.digitalWrite(R4C, LOW);//Mimic R4C RED
        mcp3.digitalWrite(R3AB, LOW);//Mimic R3AB RED
   

        SEN1_in_use = false;
        route3_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 3A from Sensor 2 to Fast Down .......

  switch (route3A_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN3) == LOW && mcp6.digitalRead(SW3) == HIGH) {
      
        mcp4.digitalWrite(TR3LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN3) == LOW && mcp6.digitalRead(SW3) == LOW) {
      
        if (mcp6.digitalRead(SW3) == LOW && !SEN3_in_use) {
          
        SEN3_in_use = true;

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP5, HIGH);//Aspect on Sen3 GREEN
        mcp3.digitalWrite(L3ABC, HIGH);//Mimic L3ABC GREEN
   
          route3A_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN3) == HIGH) { 
                      
        route3A_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN3) == HIGH) {

        mcp6.digitalWrite(PSW5, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP5, LOW);//Aspect on Sen3 RED
        mcp3.digitalWrite(L3ABC, LOW);//Mimic L3ABC RED

         SEN3_in_use = false;
        route3A_state = Idle;
        }
     }
      break;
    }
// ...... Route 2 from Sensor 6 ot Fast UP Line and Right at point 2.......

  switch (route2_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN6) == LOW && mcp6.digitalRead(SW2) == HIGH) {
      
        mcp4.digitalWrite(TR2LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN6) == LOW && mcp6.digitalRead(SW2) == LOW) {
      
        if (mcp6.digitalRead(SW2) == LOW && !SEN6_in_use) {
          
        SEN6_in_use = true;

        mcp6.digitalWrite(PSW2, LOW);//Point switch to RIGHT
        mcp1.digitalWrite(ASP6, HIGH);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR6, LOW);//Direction RIGHT
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp6.digitalWrite(PSW4, LOW);//Point 4 STRAIGHT
        mcp5.digitalWrite(PSW16, LOW);//Point 16 STRAIGHT                     
        mcp3.digitalWrite(R2ABC, HIGH);//Mimic R2ABC GREEN
   
          route2_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN6) == HIGH) { 
                      
        route2_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN6) == HIGH) {
        
        mcp6.digitalWrite(PSW2, HIGH);//Point switch to RIGHT
        mcp1.digitalWrite(ASP6, LOW);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR6, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp6.digitalWrite(PSW4, LOW);//Point 4 STRAIGHT
        mcp5.digitalWrite(PSW16, LOW);//Point 16 STRAIGHT                     
        mcp3.digitalWrite(R2ABC, LOW);//Mimic R2ABC GREEN
   

        SEN6_in_use = false;
        route2_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 2A from Sensor 2 to Fast Up and Left at Point 2 .......

  switch (route2A_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN6) == LOW && mcp6.digitalRead(SW2) == HIGH) {
      
        mcp4.digitalWrite(TR2LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN6) == LOW && mcp6.digitalRead(SW2) == LOW) {
      
        if (mcp6.digitalRead(SW2) == LOW && !SEN6_in_use) {
          
        SEN2_in_use = true;

        mcp6.digitalWrite(PSW2, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP6, HIGH);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR7, LOW);//Direction LEFT
        mcp3.digitalWrite(L3ABC, HIGH);//Mimic L3ABC GREEN
        mcp5.digitalWrite(PSW9, LOW);//Point switch to STRAIGHT
        mcp5.digitalWrite(PSW10, LOW);//Point switch to STRAIGHT
   
          route2A_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN6) == HIGH) { 
                      
        route2A_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN6) == HIGH) {

        mcp6.digitalWrite(PSW2, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP6, LOW);//Aspect on Sen6 RED
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp3.digitalWrite(L3ABC, LOW);//Mimic LBC RED
        mcp5.digitalWrite(PSW9, LOW);//Point switch to STRAIGHT
        mcp5.digitalWrite(PSW10, LOW);//Point switch to STRAIGHT

         SEN6_in_use = false;
        route2A_state = Idle;
        }
     }
      break;
    }
// ...... Route 1 from Sensor 5 ot Platform 3 UP Line and Right at point 1.......

  switch (route1_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN5) == LOW && mcp6.digitalRead(SW1) == HIGH) {
      
        mcp4.digitalWrite(TR1LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN5) == LOW && mcp6.digitalRead(SW1) == LOW) {
      
        if (mcp6.digitalRead(SW1) == LOW && !SEN5_in_use) {
          
        SEN5_in_use = true;

        mcp6.digitalWrite(PSW1, LOW);//Point switch to RIGHT
        mcp1.digitalWrite(ASP7, HIGH);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR8, LOW);//Direction RIGHT
        mcp1.digitalWrite(DIR9, HIGH);//Direction OFF
        mcp6.digitalWrite(PSW4, LOW);//Point 4 STRAIGHT
        mcp5.digitalWrite(PSW16, LOW);//Point 16 STRAIGHT                     
        mcp3.digitalWrite(R2ABC, HIGH);//Mimic R2ABC GREEN
   
          route1_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN5) == HIGH) { 
                      
        route1_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN5) == HIGH) {
        
        mcp6.digitalWrite(PSW2, HIGH);//Point switch to RIGHT
        mcp1.digitalWrite(ASP6, LOW);//Aspect on Sen6 RED
        mcp1.digitalWrite(DIR6, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp6.digitalWrite(PSW4, LOW);//Point 4 STRAIGHT
        mcp5.digitalWrite(PSW16, LOW);//Point 16 STRAIGHT                     
        mcp3.digitalWrite(R2ABC, LOW);//Mimic R2ABC RED
   

        SEN5_in_use = false;
        route1_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 1A from Sensor 5 to Platform 3 Up and Left at Point 2 .......

  switch (route1A_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN5) == LOW && mcp6.digitalRead(SW1) == HIGH) {
      
        mcp4.digitalWrite(TR1LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN5) == LOW && mcp6.digitalRead(SW1) == LOW) {
      
        if (mcp6.digitalRead(SW1) == LOW && !SEN5_in_use) {
          
        SEN5_in_use = true;

        mcp6.digitalWrite(PSW2, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP6, HIGH);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR7, LOW);//Direction LEFT
        mcp3.digitalWrite(L3ABC, HIGH);//Mimic L3ABC GREEN
        mcp5.digitalWrite(PSW9, LOW);//Point switch to STRAIGHT
        mcp5.digitalWrite(PSW10, LOW);//Point switch to STRAIGHT
   
          route1A_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN5) == HIGH) { 
                      
        route1A_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN5) == HIGH) {

        mcp6.digitalWrite(PSW2, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP6, LOW);//Aspect on Sen6 RED
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp3.digitalWrite(L3ABC, LOW);//Mimic LBC RED
        mcp5.digitalWrite(PSW9, LOW);//Point switch to STRAIGHT
        mcp5.digitalWrite(PSW10, LOW);//Point switch to STRAIGHT

         SEN5_in_use = false;
        route1A_state = Idle;
        }
     }
      break;
    }
// ...... Route 6 from Sensor 4 ot Platform 4 UP Line .......

  switch (route6_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN4) == LOW && mcp2.digitalRead(SW17) == HIGH) {
      
        mcp4.digitalWrite(TR6LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN4) == LOW && mcp2.digitalRead(SW17) == LOW) {
      
        if (mcp2.digitalRead(SW17) == LOW && !SEN4_in_use) {
          
        SEN5_in_use = true;

        mcp6.digitalWrite(PSW1, LOW);//Point switch to RIGHT
        mcp1.digitalWrite(ASP7, HIGH);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR8, LOW);//Direction RIGHT
        mcp1.digitalWrite(DIR9, HIGH);//Direction OFF
        mcp6.digitalWrite(PSW4, LOW);//Point 4 STRAIGHT
        mcp5.digitalWrite(PSW16, LOW);//Point 16 STRAIGHT                     
        mcp3.digitalWrite(R2ABC, HIGH);//Mimic R2ABC GREEN
   
          route6_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN4) == HIGH) { 
                      
        route6_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN4) == HIGH) {
        
        mcp6.digitalWrite(PSW2, HIGH);//Point switch to RIGHT
        mcp1.digitalWrite(ASP6, LOW);//Aspect on Sen6 RED
        mcp1.digitalWrite(DIR6, HIGH);//Direction OFF
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp6.digitalWrite(PSW4, LOW);//Point 4 STRAIGHT
        mcp5.digitalWrite(PSW16, LOW);//Point 16 STRAIGHT                     
        mcp3.digitalWrite(R2ABC, LOW);//Mimic R2ABC RED
   

        SEN4_in_use = false;
        route6_state = Idle;
        }
     }
      break;
    }
    
// ...... Route 6A from Sensor 4 to Platform 4 Up Line  .......

  switch (route6A_state) {

    case Idle: {
      
    if (mcp2.digitalRead(SEN4) == LOW && mcp2.digitalRead(SW17) == HIGH) {
      
        mcp4.digitalWrite(TR6LED,HIGH);    
     }     
     if (mcp2.digitalRead(SEN4) == LOW && mcp2.digitalRead(SW17) == LOW) {
      
        if (mcp2.digitalRead(SW17) == LOW && !SEN4_in_use) {
          
        SEN4_in_use = true;

        mcp6.digitalWrite(PSW2, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP6, HIGH);//Aspect on Sen6 GREEN
        mcp1.digitalWrite(DIR7, LOW);//Direction LEFT
        mcp3.digitalWrite(L3ABC, HIGH);//Mimic L3ABC GREEN
        mcp5.digitalWrite(PSW9, LOW);//Point switch to STRAIGHT
        mcp5.digitalWrite(PSW10, LOW);//Point switch to STRAIGHT
   
          route6A_state = Active;
          }         
       }
     break;
     }
     
         case Active: {
      
      if (mcp2.digitalRead(SEN4) == HIGH) { 
                      
        route6A_state = Clearing;       
        }    
      break;
      }
      
      case Clearing: {
      
      if (mcp2.digitalRead(SEN4) == HIGH) {

        mcp6.digitalWrite(PSW2, HIGH);//Point switch to LEFT
        mcp1.digitalWrite(ASP6, LOW);//Aspect on Sen6 RED
        mcp1.digitalWrite(DIR7, HIGH);//Direction OFF
        mcp3.digitalWrite(L3ABC, LOW);//Mimic LBC RED
        mcp5.digitalWrite(PSW9, LOW);//Point switch to STRAIGHT
        mcp5.digitalWrite(PSW10, LOW);//Point switch to STRAIGHT

         SEN4_in_use = false;
        route6A_state = Idle;
        }
     }
      break;
    }           
  }
   

Libaries made no difference, but when I add or subtract a number to the MCP pin I can get all but 1 to work correctly they juat seem to be triggering a different pin but there is a few common number that make it work, as per the Defined pins ...
L3ABC I cant seem to get that one working ...
Very weard, I can live with it but setting all 96 pins is not going to be easy if they are all like this so I will persivere and work out what is going on ...
I also made life easier by adding a While loop to test each Bank of LED's ...

Having #defines be an expression without parentheses is a recipe for disaster. Image this code

#define P4AB 21 - 21
int value = 100 * P4AB;

You will get 100 * 21 - 21 which yields 2079. That is one of the reasons it is much better to make constant values

const byte P4AB = 21 - 21;

or, at the very least, surround all your defines with parenthesis so you get proper order of operation

#define P4AB (21 - 21)

Thanks for posting the schematic in higher resolution, I can see what is going on here.

Now while not addressing the immediate problem I am concerned with the I2C lines. First there are no pull up resistors I can see on these lines. You only need one on each line but I see none.

Second you have a 5V I2C lines from the Arduino but are feeding this into an ESP which is a 3V3 device, so you need an I2C level shifter from the 5V I2C lines into the ESP to prevent it being damaged.

I am a great believer in getting the hardware right before considering anything else.

I think the key to your problem is, as pointed out by @blh64, is the fact that you don't use pin number 0 in all the multiplexer numbering.

Posting an image of code is less than useful, please post all code correctly.

Yes thanks for that, it was just to see if that worked so will add as you suggested, hoping I can get it working correctly and I dont need to add the error correction number ...

I really appreciate all the help i'm getting ...

John

I put 3 possible Arduino types as I didnt know how much memory the sketch would take, at the moment i'm just using a Nano and added the ESP32 which I had one to see if that would work but as the Nano only has 44% memory used I should be okay ...
The I2C lines have pull-up resistors the 2 x 4k7 R27 R28 next to IC18 CD4069 ...
The Numbers I added was just to see what happened and appart from L3ABC,

#define L3ABC 5 + 23 //Track 3 Left ABC XXX


4_Way_Mimic_Panel_Board_V2.zip (5.1 KB)

all worked with the added numbers

You keep asking for sketch but I posted twice so will try and send a zip file ...

Thanks for help I really appreciate ..

John

I think I found problem, as I was trying a Demo sketch I noticed this, you dont specify the Chip pins because the Library allocates a pin as per picture ... and that matches my first set of 8 pins ...

Yes a picture of a screen dump of your code is useless no matter how many times you post it.
You may want to read this before you proceed:-
how to get the best out of this forum
Because that tells you how to post code correctly on this forum.

John
Sometimes we're a little 'abrupt' on this forum, telling you to do things without telling you why. I'll apologize for that.
Now, as to why an image is "useless", we want you to do something else, here's the reason. I CANNOT grab your screen image and paste it into my IDE to test your code. Doesn't work that way. I need to be able to cut and paste 'text'. To facilitate that, you need to provide me with 'text'. To do that, go to your IDE, find "copy for forum" in the menu, click on that. Then, in a new post on this forum thread, Click the icon </> and then paste in the content from the IDE. That gives us your code, formatted exactly as in your IDE, so that we may then copy it to our IDE. It is essential, as it avoids all other cut-and-paste snafus.
Thanks

Yes sorry if I downloaded incorrectly I just assumed as the Forum that I Use on MERG they use the same </> brackets so I presumed it was the same with the Arduino Forum, hope the ZIP file you could open okay ...
The problem was solved as my last post as the pins are given GPIO numbers by the Library so all works including i checked the 5 TR1-5LED's, the issue with one pin not working L3ABC turned out to be IC19 socket pin 1 not soldered ... So I can know go through all the INPUT pins and change to the correct format and test ...
I will report back when I get all tested ...

Thanks for all the help...
John