Episode 39: Whistle-Script

Once again a little bit of food for programming: A “whistle in the street” script in four expansion stages:

The Concept

The script is composed of the following four elements:

  • Waiting for a feedback sensor
  • 1st whistle
  • wait a short time
  • 2nd whistle
  1. First of all, it is necessary to wait until a certain feedback triggers. The reason for this is that the script has already been started when the street is set or locked, respectively. The ‘while’-loop is there to ensure that the actions are not triggered immediately, but shortly before the railroad crossing.
  2. The first whistle/horn sound is made.
  3. A short waiting time until the second sound.
  4. The second whistle/horn sound is executed and the script is finished.

There are now some more improvements.

Improvement 1

First of all, the script should not only be usable for one locomotive, but for all those who use this route. Therefore there is a switch-case command for each loco, which queries the current locomotive from the caller (%lcid%) and switches to the current locomotive’s whistle function.

In the present case, the whistle sound is always on the function key F2 on all locomotives, so that one could actually shrink this even further (switch F2 of %lcid% on and off…) But since there will surely be a locomotive without sound decoder one day, it would be hard to play e.g. a replacement sound just for this one.

Improvement 2

Now the script looks quite good and works for different locomotives. In the long run, however, it is noticeable that all “engine drivers” react in exactly the same way and the whistle blows/horn sounds are always of the same length. This is a bit static and can be reworked using random numbers: A short random waiting time is implemented after the feedback-while-loop. So not everything always starts exactly when the feedback has triggered. Now the whistle durations and the intermediate waiting time are implemented on a random basis; of course within meaningful limits. Now it gives a much more dynamic or even more realistic impression.

Improvement 3

Finally and finally, two specific problem cases should be addressed:

  • The route takes too long (or feedback does not trigger at all),
  • The route is terminated/unlocked prematurely.

In both cases unwanted effects may occur. Therefore, this script version contains a timeout as well as a check for an adjacent street (not “locked” anymore). So even if there are delays – e.g. due to dirt – no script instances remain in memory and Rocrail runs cleanly.

Moreover, even more functions could be realized: The switching on and off of a bell or the squeaking sound in the turnouts. It is only a question of programming.

Download

The scripts are ready to download in a package:

→Download of the scripts HERE

Summary

It is always nice to see what possibilities you have with →Rocrail and the →Xml scripting. Practically the same script can be used with →this track layout plan for three routes at once: All of them lead over the level crossing.



Leave a Reply

Your email address will not be published. Required fields are marked *