Hi Malcolm,

Don't know anything about trains, but I can say with somewhat good confidence, that the waveforms you were getting are due to the Soundcard Oscilloscope.

Sound cards have a capacitively coupled input, And most have a minimum of around 20hz that they can see. That's a 20hz sine wave, not a squarewave. When you changed to pulsout, you can see on your picture that the frequency went up to almost 2khz, so it was able to at least see the waveform, even though still inaccurately.

I ran the program here (although on a different chip) and got a nice clean 40hz PWM.

It looks like you've given up on the PIC idea already so I won't bore you anymore.

But if you're going to build it out of transistors, this might help.
Various Transistor Throttles
http://home.cogeco.ca/~rpaisley4/Throttles.html

regards,