I would make this a short if I knew how but I think the aspect ratio forbids it, anyway... explanations:
You take the comparator output of a train station, and then have an observer facing something with 2 states that changes with redstone power, like a redstone torch, redstone dust, or a redstone lamp.
At the observers redstone output, have a sequenced gearshift or a redstone line that leads into your sequenced gearshift, the sequenced gearshift is set to:
1. "Do action(in this case rotate 90 degrees clockwise)"
2. "wait for another redstone pulse"
3. "Do Another action(in this case rotate 90 degrees counter-clockwise)"
4. "end"
What this means is taht when a train is in the station, the comparator turns on and a redstone signal will trigger the observer 1 time and do the first action, and then when the train leaves, the comparator will turn off, and the observer will see it turn off and send a second pulse, doing the second action in the sequenced gearshift.
![](https://i.ytimg.com/vi/66Q94jqc6PI/maxresdefault.jpg)