Go Down

Topic: would you like a program challenge? (Read 3030 times) previous topic - next topic

matelot


The final 2 signals have 5 lights and 1 switch between them, so you could manage with a single cable run for both.

The first signal and sensor is about 100 yards from the control box, the second signal and sensor is about 50 yards, the third is at the control, the forth signal and sensor is about 50 yards beyond the control and the fifth sensor is about 100 yards beyond. I will take your advise about the cable to the first four points thank you and there is a cable runs from the fifth I can use.

I have started to write a program and have yet to add a timer into each section to turn the latches low, I will need to do this to be sure the whole train has gone, perhaps there is a way I can start a count that restarts if another wheel passes and resets the latch if no wheel passes for , say, five seconds? That way I can check the train has gone through before setting the loop to accept the next train.

Code: [Select]

// run a section of railway.
// give it a name:
int switch5 = 13;
int switch4 = 12;
int switch3 = 11;
int switch2 = 10;
int switch1 = 9;
//rlys 012 drive signal 1, 345 signal 2, 67 signal 3 and 8 signal four.
int rly8 = 8;
int rly7 = 7;
int rly6 = 6;
int rly5 = 5;
int rly4 = 4;
int rly3 = 3;
int rly2 = 2;
int rly1 = 1;
int rly0 = 0;

int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;
int switchState4 = 0;
int switchState5 = 0;
int reset = 0; //set up analog pin
int latch1 = 0;
int latch2 = 0;
int latch3 = 0;
int latch4 = 0;
int latch5 = 0;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode (15, INPUT);   // use analog port as digital. set to a0 or 15 to input to read a switch
  digitalWrite (A5, HIGH);//use these two to use analog ports as digital as a reset switch
  pinMode(switch5, INPUT);
  pinMode(switch4, INPUT);
  pinMode(switch3, INPUT);
  pinMode(switch2, INPUT);
  pinMode(switch1, INPUT);
  pinMode(rly8, OUTPUT);
  pinMode(rly7, OUTPUT);
  pinMode(rly6, OUTPUT);
  pinMode(rly5, OUTPUT);
  pinMode(rly4, OUTPUT);
  pinMode(rly3, OUTPUT);
  pinMode(rly2, OUTPUT);
  pinMode(rly1, OUTPUT);
  pinMode(rly0, OUTPUT);
  digitalWrite (rly0, HIGH);
  digitalWrite (rly3, HIGH);
  digitalWrite (rly6, HIGH);
  digitalWrite (rly8, HIGH);
}

void loop() {

  // read the state of the pushbutton value:
  reset = digitalRead (15);   // read from analog reset pin
  switchState1 = digitalRead(switch1);
  switchState2 = digitalRead(switch2);
  switchState3 = digitalRead(switch3);
  switchState4 = digitalRead(switch4);
  switchState5 = digitalRead(switch5);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (reset == 1){
    digitalWrite (rly0, LOW);
    digitalWrite (rly3, LOW);
    digitalWrite (rly6, LOW);
    digitalWrite (rly8, LOW);
  }

  //SWITCH ONE
  if (latch1 == 0) {    //stops this repeating for the time loop below
    if (switchState1 == HIGH) {     //if switch one is pressed
      latch1 = (1);   
      digitalWrite(rly0, HIGH);    //turn signal 1 to red
    }    //put a time loop in here to stop it repeating then turn latch low
  }
  if (switchState1 == LOW)
    latch1 = (0);

  //SWITCH TWO
  if (latch2 == 0) {    //stops this repeating for the time loop below
    if (switchState2 == HIGH) {     //if switch two is pressed
      // turn ws1 yel and ws2 red on:
      latch2 = (1);   
      digitalWrite(rly0, LOW);     //turn signal 1 rly to next
      digitalWrite(rly1, HIGH);    //turn signal 1 to yel
      digitalWrite(rly3, HIGH);    //turn signal 2 to red
    }    //put a time loop in here to stop it repeating then turn latch low
  }
  if (switchState2 == LOW)
    latch2 = (0);

  //SWITCH THREE
  if (latch3 == 0) {    //stops this repeating for the time loop below
    if (switchState3 == HIGH) {     //if switch three is pressed
      // turn ws1 yel yel ws2 yel and ws3 red on:
      latch3 = (1);   
      digitalWrite(rly1, LOW);    //turn signal 1 rly to next
      digitalWrite(rly2, HIGH);   //turn signal 1 to yel yel
      digitalWrite(rly3, LOW);    //turn signal 2 to rly next
      digitalWrite(rly4, HIGH);   //turn signal 2 to yel
      digitalWrite(rly6, HIGH);   //turn signal 3 to red
    }    //put a time loop in here to stop it repeating then turn latch low
  }
  if (switchState3 == LOW)
    latch3 = (0);

  //SWITCH FOUR
  if (latch4 == 0) {    //stops this repeating for the time loop below
    if (switchState4 == HIGH) {     //if switch three is pressed
      // turn ws2 yel yel ws3 yel and ws4 red on:
      latch4 = (1);   
      digitalWrite(rly2, LOW);    //turn signal 1 rly to green
      digitalWrite(rly4, LOW);    //turn signal 2 rly to next
      digitalWrite(rly5, HIGH);   //turn signal 2 to yel yel
      digitalWrite(rly6, LOW);    //turn signal 3 rly to next
      digitalWrite(rly7, HIGH);   //turn signal 3 to yel
      digitalWrite(rly8, HIGH);   //turn signal 4 to red
    }    //put a time loop in here to stop it repeating then turn latch low
  }
  if (switchState4 == LOW)
    latch4 = (0);

  //SWITCH FIVE
  if (latch5 == 0) {    //stops this repeating for the time loop below
    if (switchState5 == HIGH) {     //if switch three is pressed
      // turn ws2 green ws3 green and ws4 green on:
      latch5 = (1);   
      digitalWrite(rly5, LOW);    //turn signal 2 rly to green
      digitalWrite(rly7, LOW);    //turn signal 3 rly to green
      digitalWrite(rly8, LOW);    //turn signal 4 rly to green
      digitalWrite(rly6, LOW);    //turn signal 3 rly to next
    }    //put a time loop in here to stop it repeating then turn latch low
  }
  if (switchState5 == LOW)
    latch5 = (0);


}

matelot

I think this thread should now be in the programming section? Am I right?

Henry_Best



I will need to do this to be sure the whole train has gone, perhaps there is a way I can start a count that restarts if another wheel passes and resets the latch if no wheel passes for , say, five seconds? That way I can check the train has gone through before setting the loop to accept the next train.



That isn't fail-safe. What if a train breaks down after only part of it has passed the switch? The signal for that section will go green after 5 seconds, indicating that the section is clear when it isn't. That's the reason 'timed' signalling was quickly abandoned in favour of block signalling on full size railways.

Unfortunately, most 7 1/2" and 5 1/4" gauge railways use metal sleepers [ties], so track circuiting wouldn't work, if yours is the same. You need a method to detect when the WHOLE train has cleared each section, similar to the red tail lamp on full size railways.

matelot




That isn't fail-safe. What if a train breaks down after only part of it has passed the switch? The signal for that section will go green after 5 seconds, indicating that the section is clear when it isn't. That's the reason 'timed' signalling was quickly abandoned in favour of block signalling on full size railways.

The way I have done it and the way it works with the present system is the delay only prevents the axles triggering more than once for five seconds (the time it takes a train to pass) it only goes green (or in fact through the sequence of yellow, double yellow green, one for each section away.) again when the train has gone over the next sensor and into the next section.


Unfortunately, most 7 1/2" and 5 1/4" gauge railways use metal sleepers [ties], so track circuiting wouldn't work, if yours is the same. You need a method to detect when the WHOLE train has cleared each section, similar to the red tail lamp on full size railways.

The way I was going to put a timer in was to detect every axle and reset if nothing triggered the sensor for five seconds. Admitted if a train stops then the system would fail but another train would still be one section back.
I am more interested in the program than the hardware and even though the finished program will monitor sensors going low I have programmed them as High and can change it. I have made some alterations to the timer parts so they are slightly different
Code: [Select]
//SWITCH ONE        turn signal 1 red on
 if (switchState1 == HIGH) {     //if switch one is pressed
     x1 = 0;                     //set timer to 0
     if (latch1 == 0) {    //stops this repeating for the time loop below
         latch1 = (1);
         digitalWrite(rly0, HIGH);
     }
 }
 if (switchState1 == LOW){
     x1 = x1++;
     if (x1 == 50){
         latch1 = (0);
     }
 }

I am still developing but I just thought someone would have liked a challenge.

Go Up