Hello
This may have been covered elsewhere and if so I apologize.
I have an end to end model railway with 5 blocks and 4 signals. All signals will be green to start with and the trains will only run in one direction as I have two tracks with an identical setup going the other way.
A train starts in terminus 1 block A, and passes signal A. Signal A should turn red as the train moves in to block B. At the end of block B is signal B. As the train passes signal B and passes in to block C, signal B should turn red and signal A should turn yellow.
At the end of block C is signal C. As the train passes signal C and passes in to block D, signal C should turn red, signal B should turn yellow and signal A should turn green.
At the end of block D is signal D. As the train passes signal D and passes in to block E, signal D should turn red, signal C should turn yellow, and signal B should turn green.
Block E is the final block for terminus 2 and has no further signals. There is a detector in the terminus, such that when the train passes the detector, signal D should turn yellow and signal C should turn green. After a delay of say 30 seconds to allow time for the train to come to a halt in the relevant platform, I then want signal D to change to green.
Presumably I can do all this either on several Unos or on a single Mega?
Can anyone point me to a suitable sketch that will handle this scenario or one that I can modify?
Unless you have very forgiving standards for realistic operation, you need both track current and optical detection. That is completely apart from what signal logic you use. Single point (optical) detection can be made to work but occupancy states are inferred from detection events, since those are on block boundaries. That means the system can't detect occupancy at power up. Additional logic is needed to determine direction. Whether that is sufficient for you, depends a lot on whether the signals are fully functional, or just for effect. If you have to choose between current an optical detection, current is better because it always gives you at least, the location of locomotives at the present time, without any need to guess the direction of travel. The addition of optical detection holds signals in an occupied state until non-current conducting cars can clear the block.
Lucky for you, single track ABS is relatively simple logic, and that's all your track configuration needs. When interlocking plants (e.g. points, crossovers, switches) enter the picture, things get ugly really fast.
I plan on being somewhat forgiving in that my first pass through will use infrared technology as a train passes a signal. As I progress i may well get into sensing the presence of trains within a block but fir now I just want to control the signals, with feedback back to the prior signals to turn them yellow and green respectively.
I'd like to have three types of sketch for my Arduinos. Type A controls the starting block with a signal and an infrared that triggers the signal to turn red.
Type B, has a signal and infrared that sends a message to the prior Arduino to turn yellow, and the one before that, if there is one, to green.
Type C controls the ending block and last signal on the route.
I'm also wondering how many signals I can control using an Arduino Mega.
You know of any existing sketches that might be available? You said it was relativy simple but all I've found on YouTube are videos for controlling single track lines with trains that can come in either direction. I don't have thus setup, I have double track mainline which I agree should be easier.
I think your Type A,B,C actually have the same logic, because you're describing standard ABS signalling behaviour. Because signals face in a direction, you get a direction with a detection using block boundary sensors like optical. From that you can deduce occupancy, which may be confirmed at the next boundary, or assumed by some guessed at time interval.
I don't know of any sketches although I bet there are a lot, but I have my own working. Sadly, it's not ready for release yet. Mine wouldn't help you, though, because it's track current detection based.
You could control quite a lot of signals with a Mega. Also some people use signal controllers, they use fewer wires between signal and controller. Those handle the LED or bulb drive directly. Actually those circuit concepts go back to the 1960's and relay systems as well as transistor and op amp versions floated around.
Your task is a three step process:
translate a transition into a block occupancy state
translate those block states into signal aspects
actually display them (change the lights)
With the first, your main problem is determining when a short train has left a block.
If you use arrays and/or objects, you can implement multiple blocks and handle all of them simultaneously by the same code. In that case you would need about six I/O pins per block, at least, for the LEDs (red, yellow, green) x (east, west) per block. Also several inputs for optical detectors per block.