As GRJ said, the best thing to use here is probably an Arduino or similar device. The code is not very difficult, something like this: (note that more than half of this "code" is comments describing how it works, the actual "program" that runs is only about the last 10 lines.)
int minimunLapsToTrigger = 10; // Set minimum number of laps before triggering.
int maximumLapsToTrigger = 25; // Set maximum number of laps before triggering.
int inputPin = 7; // Arduino pin connected to input circuit.
int outputPin = 8; // Arduino pin connected to output relay module.
int currentInputState = 0; // stores current state of input pin.
int lastInputState = 0; // stores last state of input pin.
int lapCounter = 0; // stores count of number of times train has passed.
int numberOfLapsBeforeTriggering = 0; // stores random number of laps before triggering.
void setup() {
// put your setup code here, to run once:
pinMode(inputPin, INPUT); // Set input pin as an input.
digitalWrite(inputPin, HIGH); // turn on internal pull-up resistor.
pinMode(outputPin, OUTPUT); // set output pin as an output
randomSeed(analogRead(A0)); // seed the random number generator with a floating value.
}
void loop() {
// put your main code here, to run repeatedly:
currentInputState = digitalRead(inputPin); // read input value into var.
// if train is there, and it was not on last loop:
if (currentInputState == 1 && currentInputState != lastInputState) {
lapCounter++; // increase lap counter by 1
}
lastInputState = currentInputState; // set last state to current one for next loop.
// if train is there and enough laps have passed:
if (lapCounter >= numberOfLapsBeforeTriggering && currentInputState == 1) {
digitalWrite(outputPin, LOW); // turn on relay, open outhouse door.
lapCounter = 0; // reset the lap counter.
// set a ner random number of laps for the next time.
numberOfLapsBeforeTriggering = random(minimunLapsToTrigger, maximumLapsToTrigger);
}
else { // if not enough laps:
digitalWrite(outputPin, HIGH); // turn relay off, outhouse door closed.
}
}
This code is untested, and needs a few tweaks in any case, like adding a de-bounce routine and such, but it shouldn't take much more than that. A couple more lines could easily add options for a manual pushbutton trigger, or whatever other options you'd like. I'd guess in a one off production all the parts to make a stand-alone, track powered, unit would cost less than $10 ordered from china, or $20 using US shipped parts. I don't think the moderators here go much for folks peddling their services, but I'm sure you could find the email address in the profiles of folks that could throw such a thing together for a nominal fee.
On the less computery end, you could use one of the timer relay modules in series with any other activator method, so that it would only allow the outhouse to be triggered every X number of minutes. it's not as fancy, but it would get the job done.
JGL