I am new to the forum and hope not out of line...

Clues:
Quote Originally Posted by Malcolm
1) - The latching is not clean, in that sometimes the LED brightens and remains on when it should be off, or when off it is turned on by the reed switch, but fails to latch
"The LED brightens" (unintentionally) does not sound very digital.
Thanks, and yes it compiles OK and in tests works as required approx 70% (ie 7 out of 10) of the time. On three attepts LED2 remained lit, but its a lot better than my 0% success rate
I hooked up a test track and placed a magnet on a loco to see how it worked, as I had been using magnets passing over the switches by hand to test the code.
I don't know if it's due to the speed of the train (slower than my hand tests) but I got a 1 in 10 success rate of both LEDs being turned off when the train exited the siding
I wonder if there might be an interaction between the magnetics of the locomotive "engine" and the reed switch. Possible even modulating the switch as the loaded motor slowly passes over the switch. Monster size "contact bounce". Or perhaps the magnetics of the motor offsets the permanent magnet, causing it to not always trigger the reed switch.

If this is part of the problem then Melanie's suggestion would certainly apply:
Quote Originally Posted by Melanie
There is another way of thinking here Alain...

Once a switch has closed (as detected by the first IF/THEN) then the assumption is that whatever event caused it to close (or open) is not a False Alarm and the event intended to happen (eg a User pressing a button). You therefore need no futher IF/THEN's to see if the contacts are still closed after the debounce time period expries.

All you have to do is delay looking at that switch again for a period of time greater than any possible contact bounce to ensure you don't register that event a second time (as would happen if you used interrupts for example).
Don't forget the sequence of permanent magnet and motor chatter would depend upon the direction of travel (just to make it interesting).

This may not be any part of Malcolm's problem but something we should keep in mind when using reed switches or hall effect devices. Don't ask why I thought of it.

This forum is the most useful resource, for beginners, on the entire web. Thanks to all you PIC gurus, most of the "gotchas" with PICs, can be found with a forum search. Thanks for all your hard work.

-Adam-