Model Rail Block detection help

I have been working on this for quite a while and eventually thought I had cracked the problem with the operation and having added 2 out of 6 Detection Blocks I thought I had cracked it but ... Murphy gets you every time ...
Got Detection Block working fine on its own ... aded the extra code for Detection block 2 and works fine ... BUT ... I fine that as Detection Block 1 the pointswp1 goes LOW when not in use so when Detection Block 2 operation pointswp1 LED comes on and goes off (using LEDs to test) and this is because it see's pointswp1 in Detection 1 is LOW so that is the problem ...
SO my best option is to change the Void Loops into State machine i.e Switch Case but I am unsure how to start to modify the sketch using the enum and switch statements ... I am a novice programmer so would be grateful for help ..
John

void loop() {

  BlockState1 = digitalRead(BlockDet1);
  BlockState2 = digitalRead(BlockDet2);
  BlockState3 = digitalRead(BlockDet3);
  BlockState4 = digitalRead(BlockDet4);
  BlockState5 = digitalRead(BlockDet5);
  BlockState6 = digitalRead(BlockDet6);
  BlockState7 = digitalRead(BlockDet7);
  BlockState8 = digitalRead(BlockDet8);
  BlockState9 = digitalRead(BlockDet9);
  BlockState10 = digitalRead(BlockDet10);
 
   Buttonval1 = digitalRead(P1button);  // Read the Button 1 Value
   Buttonval2 = digitalRead(P2button);  // Read the Button 2 Value
   Buttonval3 = digitalRead(P3button);  // Read the Button 3 Value
  
  //#################################### Detection 1 Sensor 6 #############################

  // Checking Block State operation
  // Track 1 South leading to Darcy Loop via Point P1
  // Sensors Used - Sensor 6
 
  Serial.print("    ");
  Serial.print("Button Value   ");
  Serial.println(Buttonval1);
  Serial.print("Block State 6    ");
  Serial.print(BlockState6);
  
  if (BlockState6 == HIGH && Buttonval1 == HIGH ) {  //Start of Sensor 6 detection Output of Timer Block goes HIGH when detection of Sensor 6
     digitalWrite (senseLed1, HIGH);// Turns Mimic panel Sense 1 LED ON
  }
  
    if (BlockState6 == HIGH && Buttonval1 == LOW ) {  //Start of Sensor 6 detection Output of Timer Block goes HIGH when detection of Sensor  

   //All point operation is via a HIGH/LOW input via an input resistor on a 2N7000 switching a Servo via a MERG SERVO4 controller 
   //Also to a Nano control for Mimic Panel LED's

    digitalWrite(pointswp1, HIGH); // Operate Points P1,   Left to Darcy Loop
    
    delay(200); //Delay for Red to Green Signal
        
    digitalWrite(signalPinsBicol1,LOW);// Turn GREEN Signal ON     
  }
    

  if (BlockState6 == LOW) { //From Sensor 6 Entry detection Sensor 10 Holds Timer Output HIGH till Last Carraige has Exit Block
       
     digitalWrite(signalPinsBicol1,HIGH);// Turn Red Signal ON
     
  //  digitalWrite(pointswp1, LOW); // Operate Points P1, Back to Straight ahead position  
   
    digitalWrite (senseLed1, LOW); // Turn Mimic panel Sense LED off   
  } 
  //#################################### Detection 2 Sensor 3 ############################# 

  // Checking Block State operation
  // Darcy Loop leading to Track 4
  // Sensors Used

  if (BlockState3 == HIGH && Buttonval2 == HIGH) {  // Start of IR Sensor 3 detection Output of Timer Block goes HIGH when detection of IR Sensor 
     
    digitalWrite(senseLed2, HIGH);// Turns Mimic panel Sense 2 LED ON
  }  

    if (BlockState3 == HIGH && Buttonval2 == LOW ) {  //Start of Sensor 6 detection Output of Timer Block goes HIGH when detection of Sensor  {
   
    
     
   
  //All point operation is via a HIGH/LOW input via an input resistor on a 2N7000 switching a Servo via a MERG SERVO4 controller 
  //Also to a Nano control for Mimic Panel LED's
  
    digitalWrite(pointswp1, HIGH); // Operate Points P1 To Left from Darcy Loop
    digitalWrite(pointswp2, HIGH); // Operate Points P2 To Left from Darcy Loop
    digitalWrite(pointswp6, HIGH); // Operate Points P6 To Left from Track 2 to 4
    digitalWrite(pointswp7, HIGH); // Operate Points P7 To Left Double Slip
    digitalWrite(pointswp8, HIGH); // Operate Points P8 To Left Double Slip    
    digitalWrite(pointswp9, HIGH); // Operate Points P9 To Left to line 4 from Double Slip
    
    delay(500); //Delay for Signal Red to Green
     
    digitalWrite(signalPinsBicol2,LOW);// Turn GREEN Signal ON   
  }
  
     

  if (BlockState3 == LOW) {  //   From Sensor 3 Entry detection Sensor 5 Holds Timer Output HIGH till Last Carraige has Exit Block
   
    digitalWrite(signalPinsBicol2,HIGH);// Turn Red Signal ON
     
    digitalWrite(pointswp1, LOW); // Operate Points P1 back to Straight
    digitalWrite(pointswp2, LOW); // Operate Points P2 back to Straight
    digitalWrite(pointswp6, LOW); // Operate Points P6 back to Straight
    digitalWrite(pointswp7, LOW); // Operate Points P7 back to Straight
    digitalWrite(pointswp8, LOW); // Operate Points P8 back to Straight
    digitalWrite(pointswp9, LOW); // Operate Points P9 back to Straight
    
    digitalWrite(senseLed2, LOW); // Turn Mimic panel Sense LED off
     
  }
}

Post what you have at the moment - it'll make its much easier to advise.

Wouldnt let me post as over 9000 characters so split ...

//  PCF8574 outputs for point switching
int P1button = 6; //P0 output P0 to P6 Working Buttons
int P2button = 7; //P1 output
int P3button = 8; //P2 output
int P4button = 9; //P3 output
int P5button = 10; //P4 output assigned to Shunt points switching ?????
int P6button = 11; //P5 output
int P7button = 12; //P6 output Not assigned
int P8button = 13; //P7 output Not assigned  Correct 18/10/20203


//  Sensor IR or PMP7 or TOTi Detection 
int BlockDet1 = 22; //Entry On Fast Up Line .. Exit Sensor Sen5 or 6
int BlockDet2 = 23; //Entry On Slow Up Line .. Exit Sensor Sen5 
int BlockDet3 = 24; //Entry On Loop Line to Fast Down Line .. Exit Sensor Sen6 or Sen5
int BlockDet4 = 25; //Entry On Fast Down Line .. Exit Sensor Sen7 ....IR Sensor input pins
int BlockDet5 = 26; //Exit On Slow Up Line .. Entry Sensor Sen 2 or 1
int BlockDet6 = 27; //Exit On Fast Up Line .. Entry Sensor Sen 1 or 3
int BlockDet7 = 28; //Exit On Fast Down Line .. Entry Sensor 4
int BlockDet8 = 29; //Exit On Slow Down Line .. Entry Sensor Not yet defined ..... Correct 18/10/2020
int BlockDet9 = 30; //  
int BlockDet10 = 31;// 

//  Aspect Signal outputs
int signalPinsBicol1 = 32; //On Fast Up Line .. Bi Colour Signals pins
int signalPinsBicol2 = 33; //On Slow Up Line
int signalPinsBicol3 = 34; //On Darcy Loop Line
int signalPinsBicol4 = 35; //On Fast Down Line
int signalPinsBicol5 = 36; //On Slow Down Line .. Bi Colour Signals pins
int signalPinsBicol6 = 37; //On Slow Down Line from Platform 1
int signalPinsBicol7 = 38; //On Darcy Loop Line ... Correct 18/10/2020

// Mimic Panel Sense Display Yellow LEDs
int senseLed1 = 48; //Sense LED on Fast up line ...sense LEDs to show on mimic panel when triggered
int senseLed2 = 49; //Sense LED on Slow up Line
int senseLed3 = 50; //Sense LED on Loop Line
int senseLed4 = 51; //Sense LED on Fast Down Line
int senseLed5 = 52; //Sense LED on Slow Down Line ...As yet not used
int senseLed6 = 53; //Sense LED on Platform 1 Down ... As yet not used... Correct 18/10/2020

//  Point Switching Outputs
int pointswp1 = 40; //On LH point on Slow Down Line To Darcy Lever Loop.. Point switching pins
int pointswp2 = 41; //On 3 Way point from Darcy Loop Line
int pointswp3 = 42; //On 3 Way point crossing Fast Down to Fast Up Lines
int pointswp4 = 43; //On RH point from Fast Up to 3 Way point
int pointswp5 = 44; //On RH Pointfrom Platform 1 to Slow Down Line .. Point switching pins
int pointswp6 = 45; //On LH point on Fast Up Line
int pointswp7 = 46; //On DS point leaving DS on Fast Up Line 
int pointswp8 = 47; //On DS point on Slow Up from DS Up Line
int pointswp9 = 39; //On Slow Up Line

int BlockState1 = 0;
int BlockState2 = 0;
int BlockState3 = 0;
int BlockState4 = 0;
int BlockState5 = 0;
int BlockState6 = 0;
int BlockState7 = 0;
int BlockState8 = 0;
int BlockState9 = 0;
int BlockState10 = 0;

int Buttonval1 = 0;
int Buttonval2 = 0;
int Buttonval3 = 0;
int Buttonval4 = 0;
int Buttonval5 = 0;
int Buttonval6 = 0;
int Buttonval7 = 0;
int Buttonval8 = 0;

 


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

  
  pinMode(BlockDet1,INPUT_PULLUP); //IR sensor Inputs or PMP7 or TOTi
  pinMode(BlockDet2,INPUT_PULLUP);
  pinMode(BlockDet3,INPUT_PULLUP);
  pinMode(BlockDet4,INPUT_PULLUP); 
  pinMode(BlockDet5,INPUT_PULLUP); //IR sensor Inputs or PMP7 or TOTi
  pinMode(BlockDet6,INPUT_PULLUP);
  pinMode(BlockDet7,INPUT_PULLUP);
  pinMode(BlockDet8,INPUT_PULLUP);
  pinMode(BlockDet9,INPUT_PULLUP);
  pinMode(BlockDet10,INPUT_PULLUP);
 

    pinMode(senseLed1,OUTPUT); // All on Mimic Panel
    pinMode(senseLed2,OUTPUT);
    pinMode(senseLed3,OUTPUT);
    pinMode(senseLed4,OUTPUT);
    pinMode(senseLed5,OUTPUT);
    pinMode(senseLed6,OUTPUT);

    pinMode(signalPinsBicol1, OUTPUT);// RED Green Bi-Colour LEDs for 6 Signals
    pinMode(signalPinsBicol2, OUTPUT);
    pinMode(signalPinsBicol3, OUTPUT);
    pinMode(signalPinsBicol4, OUTPUT);
    pinMode(signalPinsBicol5, OUTPUT);
    pinMode(signalPinsBicol6, OUTPUT);
//pinMode(signalPinsBicol7, OUTPUT);
//pinMode(signalPinsBicol8, OUTPUT);

    pinMode(P1button, INPUT_PULLUP);// Input Pushbuttons to operate points
    pinMode(P2button, INPUT_PULLUP);
    pinMode(P3button, INPUT_PULLUP);
    pinMode(P4button, INPUT_PULLUP);
    pinMode(P5button, INPUT_PULLUP);// Shunt Input
    pinMode(P6button, INPUT_PULLUP);
//  pinMode(P7button, INPUT_PULLUP);
//  pinMode(P8button, INPUT_PULLUP);

    digitalWrite(signalPinsBicol1, HIGH);
    digitalWrite(signalPinsBicol2, LOW);
    digitalWrite(signalPinsBicol3, LOW); 
    digitalWrite(signalPinsBicol4, LOW);
    digitalWrite(signalPinsBicol5, LOW);
    digitalWrite(signalPinsBicol6, LOW);
//digitalWrite(signalPinsBicol7, LOW);  
//digitalWrite(signalPinsBicol8, LOW);

    digitalWrite(senseLed1,HIGH);
    digitalWrite(senseLed2,HIGH);
    
    pinMode(senseLed2,OUTPUT);
    pinMode(senseLed3,OUTPUT);
    pinMode(senseLed4,OUTPUT);
    pinMode(senseLed5,OUTPUT);
    pinMode(senseLed6,OUTPUT);

    digitalWrite(P1button,HIGH);
    digitalWrite(P2button,HIGH);
    digitalWrite(P3button,HIGH);
    digitalWrite(P4button,HIGH);
    digitalWrite(P5button,HIGH);// Shunt setting 
    digitalWrite(P6button,HIGH);

    pinMode(pointswp1, OUTPUT);// OUTPUTS to switch Point Servo via 2N7000 and Servo4 controller
    pinMode(pointswp2, OUTPUT);
    pinMode(pointswp3, OUTPUT);
    pinMode(pointswp4, OUTPUT);
    pinMode(pointswp5, OUTPUT);// OUTPUTS to switch Point Servo via 2N7000 and Servo4 controller
    pinMode(pointswp6, OUTPUT);
    pinMode(pointswp7, OUTPUT);
    pinMode(pointswp8, OUTPUT);
    pinMode(pointswp9, OUTPUT);

 

   

}

The correct way to add a sketch that is too large, is to add it as an attachment.

as Detection Block 1 the pointswp1 goes LOW when not in use so when Detection Block 2 operation pointswp1 LED comes on and goes off (using LEDs to test) and this is because it see's pointswp1 in Detection 1 is LOW

This language is extremely confusing. You need to elaborate and re-phrase in terms of someone who is not familiar with your system. Without a clear problem description, it will be nearly impossible to help you.

I tried that with a Notepad++ file and it rejected that...

John40131:
I tried that with a Notepad++ file and it rejected that...

Why would you post a Notepad++ file? Just post the .ino

Here is an example:

BareMinimum.ino (129 Bytes)

Well I tried to post a Notepad++ file because I didnt know it wouldnt accept ... I would appreciate help not critisism ..I dont use this forum very much so thats why..

John40131:
Well I tried to post a Notepad++ file because I didnt know it wouldnt accept ... I would appreciate help not critisism ..I dont use this forum very much so thats why..

I told you how to post, which you were having trouble with. Isn't that help? I'm assuming you must have the .ino file in an IDE sketch folder since you're working on it.

Any time you have variables with numeric suffixes e.g. pointswp1, it tends to be an indication that you should consider arrays.

I don't know how model railway controllers generally work, but it seems odd that there are multiple places in the code that switch a set of points independently. I would have expected one place only that considers all the inputs.

What's the process (in English) to decide what the points setting should be?

John, as others have already mentioned you should read about array's and why they will help you.

ADDITIONALLY: if you find variables which are in relation put them into a struct.
the same amount of your variable numbering

BlockState1 .. 10 and
BlockDet1 .. 10

indicate, that these variables are in relationship.
Also a struct can be kept as array.
So before you redesign your code using a FSM (or several FSM for several blocks/buttons/...), learn some basics about structs and arrays.

Summary: Using arrays will shorten your code, reduce the possibility of coding errors and make the code more readable.

John40131:
SO my best option is to change the Void Loops into State machine i.e Switch Case but I am unsure how to start to modify the sketch using the enum and switch statements

huh?

a better explanation of your problem would help? what is "pointswp1"?

The detection is via IR Sensors ...i.e Entry and Exit...
When an Entry snsor is detected on a particular line a Yellow LED on a Mimic panel is lit up ... I then press the appropriate Button P1 to P6 and that sets points to whichever direction is set in sketch and Signal is changed from Red to Green ( Bi-Colour) after the train exits the Exit sensor the Exit IR sensor changes points and signals back to what were previous ... the Entry / Exit Sensor detection is fed via a PCB with a 555 timer and other logic so exit sensor doesnt changed points back to early ...
The Junction etc as per picture ...

noiasca:
John, as others have already mentioned you should read about array's and why they will help you.

ADDITIONALLY: if you find variables which are in relation put them into a struct.
the same amount of your variable numbering

BlockState1 .. 10 and
BlockDet1 .. 10

indicate, that these variables are in relationship.
Also a struct can be kept as array.
So before you redesign your code using a FSM (or several FSM for several blocks/buttons/...), learn some basics about structs and arrays.

Summary: Using arrays will shorten your code, reduce the possibility of coding errors and make the code more readable.
I find Arrays to complicated ... I do understand that the Array would work but I want to get this working fairly soon not tearing my hair out with a problem I cant solve ...

I find Arrays to complicated ... I do understand that the Array would work but I want to get this working fairly soon not tearing my hair out with a problem I cant solve ...

I guess guys like you are smart enough to deal with arrays.
In the end it isn't so much difference between:

digitalWrite(pointswp1, LOW); // Operate Points P1 back to Straight

vs.

digitalWrite(pointswp[1], LOW);

do you see this "change" ? That's basically all *). There are just some additional things you have to care about, but basically it's not a miracle.

or see thes 9 lines:

    pinMode(pointswp1, OUTPUT);// OUTPUTS to switch Point Servo via 2N7000 and Servo4 controller
    pinMode(pointswp2, OUTPUT);
    pinMode(pointswp3, OUTPUT);
    pinMode(pointswp4, OUTPUT);
    pinMode(pointswp5, OUTPUT);// OUTPUTS to switch Point Servo via 2N7000 and Servo4 controller
    pinMode(pointswp6, OUTPUT);
    pinMode(pointswp7, OUTPUT);
    pinMode(pointswp8, OUTPUT);
    pinMode(pointswp9, OUTPUT);

they will get a two liner

    for (byte i = 0; i < sizeof(pointswp); i++)
       pinMode(pointswp[i], OUTPUT);

Still not convinced?

*) simplified as teaser... we will explain index numbering later...

for (auto & point : pointswp )
       pinMode(point, OUTPUT);

TheMemberFormerlyKnownAsAWOL:

for (auto & point : pointswp )

pinMode(point, OUTPUT);

Wait, I don't understand the first line at all... please explain?

noiasca:
I guess guys like you are smart enough to deal with arrays.
In the end it isn't so much difference between:

digitalWrite(pointswp1, LOW); // Operate Points P1 back to Straight

vs.

digitalWrite(pointswp[1], LOW);

do you see this "change" ? That's basically all *). There are just some additional things you have to care about, but basically it's not a miracle.

or see thes 9 lines:

    pinMode(pointswp1, OUTPUT);// OUTPUTS to switch Point Servo via 2N7000 and Servo4 controller

pinMode(pointswp2, OUTPUT);
   pinMode(pointswp3, OUTPUT);
   pinMode(pointswp4, OUTPUT);
   pinMode(pointswp5, OUTPUT);// OUTPUTS to switch Point Servo via 2N7000 and Servo4 controller
   pinMode(pointswp6, OUTPUT);
   pinMode(pointswp7, OUTPUT);
   pinMode(pointswp8, OUTPUT);
   pinMode(pointswp9, OUTPUT);




they will get a two liner


for (byte i = 0; i < sizeof(pointswp); i++)
      pinMode(pointswp[i], OUTPUT);




Still not convinced?

My point is it doesnt get round this issue ... in the Detection 1 and 2 parts when the program loops to BlockState3 and triggers pointswp1 through to 9 to HIGH


if (BlockState6 == HIGH && Buttonval1 == HIGH ) {  //Start of Sensor 6 detection Output of Timer Block goes HIGH when detection of Sensor 6
    digitalWrite (senseLed1, HIGH);// Turns Mimic panel Sense 1 LED ON
  }
 
    if (BlockState6 == HIGH && Buttonval1 == LOW ) {  //Start of Sensor 6 detection Output of Timer Block goes HIGH when detection of Sensor

//All point operation is via a HIGH/LOW input via an input resistor on a 2N7000 switching a Servo via a MERG SERVO4 controller
  //Also to a Nano control for Mimic Panel LED's

digitalWrite(pointswp1, HIGH); // Operate Points P1,  Left to Darcy Loop
   
    delay(200); //Delay for Red to Green Signal
       
    digitalWrite(signalPinsBicol1,LOW);// Turn GREEN Signal ON   
  }

if (BlockState6 == LOW) { //From Sensor 6 Entry detection Sensor 10 Holds Timer Output HIGH till Last Carraige has Exit Block
     
    digitalWrite(signalPinsBicol1,HIGH);// Turn Red Signal ON
   
  //  digitalWrite(pointswp1, LOW); // Operate Points P1, Back to Straight ahead position 
 
    digitalWrite (senseLed1, LOW); // Turn Mimic panel Sense LED off 
  }
  //#################################### Detection 2 Sensor 3 #############################

// Checking Block State operation
  // Darcy Loop leading to Track 4
  // Sensors Used

if (BlockState3 == HIGH && Buttonval2 == HIGH) {  // Start of IR Sensor 3 detection Output of Timer Block goes HIGH when detection of IR Sensor
   
    digitalWrite(senseLed2, HIGH);// Turns Mimic panel Sense 2 LED ON
  }

if (BlockState3 == HIGH && Buttonval2 == LOW ) {  //Start of Sensor 6 detection Output of Timer Block goes HIGH when detection of Sensor  {
 
   
   
 
  //All point operation is via a HIGH/LOW input via an input resistor on a 2N7000 switching a Servo via a MERG SERVO4 controller
  //Also to a Nano control for Mimic Panel LED's
 
    digitalWrite(pointswp1, HIGH); // Operate Points P1 To Left from Darcy Loop
    digitalWrite(pointswp2, HIGH); // Operate Points P2 To Left from Darcy Loop
    digitalWrite(pointswp6, HIGH); // Operate Points P6 To Left from Track 2 to 4
    digitalWrite(pointswp7, HIGH); // Operate Points P7 To Left Double Slip
    digitalWrite(pointswp8, HIGH); // Operate Points P8 To Left Double Slip   
    digitalWrite(pointswp9, HIGH); // Operate Points P9 To Left to line 4 from Double Slip



.. then the program loops round again and see's in BlockState6 its telling pointswp1 to change back to LOW because BlockState 6 is LOW 


*) simplified as teaser... we will explain index numbering later...

My point is it doesnt get round this issue

Perhaps, however it might produce some code that people are more willing to work on for free. Also, often a good cleanup and adoption of common programming practices leads directly to a solution without any outside help. I've seen it happen frequently.

Create a reference for each element of "pointswp"