Build the circuit
The circuit starts with a power bit connected to a long LED. The LED illuminates a light sensor. The long LED's output is connected to the light sensor's input to power the sensor. The light sensor is in DARK mode. When the light is blocked, the sensor's output goes high, triggering the number bit to count up. The output of the number bit is connected to a threshold. As soon as the threshold is reached, two things happen (via a split wire).
The first thing is that the number bit is reset to zero using the reset input on top of the number bit. This way you can have the number bit count up until a certain number, then reset, then start counting up again. Over and over.
The second thing that happens is that a timeout is triggered. The timeout is in OFF-ON mode, meaning that it turns off for a certain time, then turns on again.
This causes the servo to move to its OFF position, flipping the track switch. After a delay, the servo moves back to its ON position. The ON position of the servo is determined by the slider. The number bit after the slider is set to "read value" to show the servo position.
The above is the essence of the circuit. However, sometimes when the servo moves, there is interference on the signal line causing the servo to move back and forth a few times before settling. I solved this by decoupling the servo from the rest of the circuit with a wireless transmitter/receiver. The output of the number bit goes into the wireless transmitter, and the servo is connected to the wireless receiver. This obviously requires an extra power bit and power source.
Adjust all the knobs
This is the tricky part. There are 4 settings to adjust.
1. Light sensor. Adjust the sensitivity to the ambient light. The sensor should trigger just once for the entire train. If it is slightly too sensitive, it will trigger with each wagon.
2. Threshold. This determines how many rounds the train does on one track before moving to the other track.
3. Timeout. The timeout should switch the servo long enough so that the train has fully passed the track switch, but before it reaches the track switch again. Unless you want to train your LEGO rescue team. The timeout is VERY difficult to adjust for short time spans. Move it by fractions of a millimeter...
4. Slider. The slider determines how far the servo moves. It should move far enough to trigger a track switch (the LEGO switches require quite some force!).
Enjoy your automated LEGO train control system
Marvel at your creation. You may want to demo this with less than 4 rounds on the first track, otherwise your audience may get impatient :-)