In the previous video we simulated wagon loading by advancing the train one wagon at a time, based on a fixed speed during a certain amount of ms. This works well if the engine runs stable at low speeds and if the number of wagons in a train does not change throughout a driving session.
If one of these conditions is not met, we can use a sensor to detect the wagons. This could be a light beam that detects the gap between the wagons, or a reed switch on the track combined with a magnet under every wagon. The end of the train is assumed if no new wagion is detected after a given number of seconds.
The macro that advances the engine has the following code:
1. Switch the ‘Loading’ switch on.
2. Label ‘Next’. This is the point where the below Goto in line 7 jumps to.
3. Increase the wagon counter.
4. Engine speed 6.
5. Delay 00:00:15:000. Total time for advancing plus loading.
6. Prerequisite on/off switch ‘Loading’. If not end of train, execute the goto statement.
7. Goto label ‘Next’. Jump back to the top and repeat the sequence.
The sensor is given an Operation that stops the train. Furthermore we use 2 flagmen to detect the end of the train. The first Flagman is the ‘Timer’. It triggers when the detector sees the gap between wagons and in its Memory tab it is put on ‘Timer reset’ after a given number of seconds. Be sure to set the tick mark at ‘Forced Reset’. The second Flagman is the ‘End of Train’ flag, it is triggered by the Timer Flagman’s reset, with the conition that the sensor still sees no new wagon. In its Operations we turn off the ‘Loading’ switch, which means the prerequisite in the macro is no longer true and the goto statement is skipped … this ends the macro.