Model Railway Operation

I am needing some help on a Arduino operated model railway project that operates the signals. The project is that if train drives over the light sensor and gives a "LOW" reading to change the signal ahead to green. the problem is when the train is diving in the opposite direction it also gives a green light. Is there a way to use sequences if the first sensor is triggered then the second sensor is triggered the signal will be green whereas if the second sensor is triggered before the first the signal will remain red? I will be very grateful of any help. James

Is there a way to use sequences if the first sensor is triggered then the second sensor is triggered the signal will be green whereas if the second sensor is triggered before the first the signal will remain red?

Yes, of course. Post the code that you have so far for one sensor to turn the signal green. By the way, that sounds illogical. Should it not turn red to stop following trains colliding with the first one ?

Are you using 12V DC to drive your trains? If so, you can detect which rail is the more positive one to tell which direction your train is travelling in.

Henry_Best: Are you using 12V DC to drive your trains? If so, you can detect which rail is the more positive one to tell which direction your train is travelling in.

But be careful not to connect negative voltages to an Arduino OR any positive voltage above 5v.

...R

const int switchPin = 2;
const int switchPin2 = 3;
const int motorPin = 9;
int switchState = 0;
void setup() {
  pinMode(motorPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(switchPin2, INPUT);
}
void loop(){
  switchState = digitalRead(switchPin);
  if (switchState == LOW) {
    digitalWrite(motorPin, HIGH);
    delay(5000);
  }
  else{
    digitalWrite(motorPin, LOW);
  }
}

UKHeliBob, I would have two of these at each end of the station to control the direction an just one track. Henry_Best, Yes. How would I do this?

If your eventual program is much longer than the piece you have posted you will almost certainly run into problems using the delay() function which blocks all other Arduino activity.

Look at how to manage time without blocking using millis() as shown in the Blink Without Delay example sketch and in the demo several things at a time.

...R

Or a State Machine.

Certainly with trains buzzing around, and needing to flash crossing lights, and change signals, putting in delays will be the last thing you need.

I would have two of these at each end of the station to control the direction an just one track.

Sorry but I am still not getting it. I will, however, watch this thread with interest.

I'm new here and still learning.. But with the provided code, you haven't checked the state of the second input..

If I understand correctly, you have 2?? trains on the same track running in different directions. In order to keep them from crashing, you want to stop one train if the other is coming???

When a train trips the first sensor, you need to check the other sensor before switching states(green light)... if the other one has been tripped before this one.. turn the drive motor off.. if not, full steam ahead...

If I understand correctly, you have 2?? trains on the same track running in different directions. In order to keep them from crashing, you want to stop one train if the other is coming???

If I were on one of the trains I would prefer it if both of them were stopped.

UKHeliBob: If I were on one of the trains I would prefer it if both of them were stopped.

You're just no fun at all! ;)

UKHeliBob: If I were on one of the trains I would prefer it if both of them were stopped.

If your riding model trains, your a lot younger than I thought :)

After thinking about it a minute.. and rereading the post a few times, I'm not sure what he wants to do.. What ever it is though, he needs to get the state of the other input..

JamesBozeat: Henry_Best, Yes. How would I do this?

With 2 opto-couplers and 2 dropping resistors for the LEDs in them. Connect them across the track so that the LED in one is the reverse way to the LED in the other. Only one of the outputs will conduct, depending on which rail is more positive than the other. There's a snag, though. Neither will conduct when there's no power to the track! But that's easily overcome in software by using a variable to remember which was the last direction of running.

kycountry: I'm new here and still learning.. But with the provided code, you haven't checked the state of the second input..

If I understand correctly, you have 2?? trains on the same track running in different directions. In order to keep them from crashing, you want to stop one train if the other is coming???

When a train trips the first sensor, you need to check the other sensor before switching states(green light)... if the other one has been tripped before this one.. turn the drive motor off.. if not, full steam ahead...

Nothing as complicated as that. He wants to drive one train on a single track, first in one direction then, some time later, in the other direction and have the correct signals light up for the direction of travel.

JamesBozeat: UKHeliBob, I would have two of these at each end of the station to control the direction an just one track.

If you have two LDRs (A & B) almost next to each other it's simply a matter of determining which of the two goes dark first to determine the direction of travel. A goes dark before B means you're going A-->B--------> and onward. B goes dark before A means you're going B-->A--------> and onward. You'll need a pair of LDRs like this at each end of your layout.

Not connected, just curious: What scale/gauge are you modelling in?

here’s a layout hope you can understand it
I am trying to incorporate Robin2’s idea.
It is 00 gauge.
I could include a led next to the light dependant resistor but this would only work on the old style of control with the new DCC control a constant voltage is applied to the track in only one direction all the time.

I'm not sure what your diagram has to do with my suggestion to use non-blocking code ???

The logic of your diagram seems reasonable.

I presume you (and not the computer) are controlling the trains. The reason I say that is because, if the computer is controlling the trains it will "know" which train it is controlling and which direction it is going and direction detection would be unnecessary.

...R

I presume you (and not the computer) are controlling the trains. The reason I say that is because, if the computer is controlling the trains it will "know" which train it is controlling and which direction it is going and direction detection would be unnecessary.

Correct. I have added the extra input because I believed I would require another sensor.

JamesBozeat: I have added the extra input because I believed I would require another sensor.

Sorry - what extra input?

...R

Another input for a sense of direction when one is triggered first then the other you can tell the direction.