toying with an idea and would like some input

pfroment

New Member
I am a model railroader in n scale. In n scale 33 ft is one scale mile. I want to determine how fast a train is going.
My idea is to put a photo resistor under the track and 33 ft away a second photo resistor. When the train passes over the first sensor, it would start a clock and when it passes over the second, the clock would stop. Using the elapsed time i can determine the speed. How can i set this idea into motion. i honestly have no idea how to begin to put this into code.
Any ideas?
 

westaust55

Moderator
Do you really need to use a 33 ft length?
That is quite long but depends on whether you want a longer term average or a relatively instantaneous speed.
Maybe down to around 3 ft or even 1 ft (304 mm) may suffice.

Another alternative could be to consider a speedometer wagon.
This one is set up for 00 gauge (1:76) and in n gauge (1:144) you may need a relatively long wagon to put two AAA batteries end to end rather than side by side.
http://www.picaxeforum.co.uk/showthread.php?27013-PICAXE-based-Model-Railway-Speedometer-Wagon

What era are you modelling? That is steam or diesel or electric?
Would have a bearing upon scale max speeds and what distance can give a good measurement.

What level of accuracy are you anticipating?
1 mph or 1 kph would be reasonable and maybe 0.5 mph/kph below 10 mph/kph.

EDIT:
Further thoughts/queries for a fixed type device:
Will you want to measure speeds in both directions?
What will the lighting level be like? If the room is darker (eg modelling night operations) on some occasions the photo resistor may not detect (sufficiently) the light change.
Might an IR LED and photo transistor mounted across the track so that the train cuts the beam and then ambient lighting level become less relevant be worth considering.

For a shorter distance such as 12 inches, a portable assembly/tunnel that you place over the track might be worth consideration.
 
Last edited:

abenn

Senior Member
I'll be watching this thread with interest, but if you're going to measure scale speeds you need to be careful about the scale.

The OP's 33ft per mile is correct for the US 1:160 scale N-gauge, but in the UK N-gauge is 1:148 scale, which gives 35.676 feet for a mile.
 

Reloadron

Senior Member
I am a model railroader in n scale. In n scale 33 ft is one scale mile. I want to determine how fast a train is going.
My idea is to put a photo resistor under the track and 33 ft away a second photo resistor. When the train passes over the first sensor, it would start a clock and when it passes over the second, the clock would stop. Using the elapsed time i can determine the speed. How can i set this idea into motion. i honestly have no idea how to begin to put this into code.
Any ideas?
Since Velocity = Distance / Time I would assume you could space two sensors maybe 2 or 4 feet apart (the greater the distance the better the measurement) and allow the sensors to Start / Stop a digital timer in your PICAXE. Then do the math to convert your velocity (speed) to whatever you wish. For example aside from a train. If I shoot a rifle bullet across two sensors spaced 2' apart and measure the time as 714.3 uSec I would get Velocity = Distance (2') / .0007143 Sec. = 2800 FPS (Foot Per Second) or roughly 0.5303 mile per second (US) as 5,280 feet = 1.0 mile. So 2800/5280 = .530303 mile. So 0.530303 * 60 = 31.818 miles per min and * 60 = 1,909 Miles Per Hour. That speed would need scaled to model RR units and if a train would be a fast train. :)

Anyway, I would work from Velocity or Speed = Distance / Time

I may be way off base with all of this and if so my apology.

Ron
 

lbenson

Senior Member
As it happens, my very first post on this forum, December 21, 2006, was on a related issue: a "speed trap" for model ships, started by wrenow: http://www.picaxeforum.co.uk/showthread.php?5794-Speed-Trap

The program at the end provides some code. Apologies because formatting (indentation) of programs from that long ago is lost. Apologies also for the structure of the program--picaxe basic of that era lacked some of the superior program structures which it has now, so GOTOs are in evidence.
 

pfroment

New Member
I am glad to see some interest in my post. For those who do not dabble in model railroading, i will further explain my goal.
Each Locomotive has a decoder installed. that is a computer chip which allows me to adjust over 100 control variables.
A set is designated for speed. I want all my locos to run at a top scale speed of 40 mph, hence the timer circuit. picture , if you will, a $400 steam loco not set up for top speed. Some cranks the throttle open, the loco screams off at 250 mph. It enters a curve, slams to the floor in a thousand pieces. I will check out the post as suggested and keep you informed.
 

Reloadron

Senior Member
I am glad to see some interest in my post. For those who do not dabble in model railroading, i will further explain my goal.
Each Locomotive has a decoder installed. that is a computer chip which allows me to adjust over 100 control variables.
A set is designated for speed. I want all my locos to run at a top scale speed of 40 mph, hence the timer circuit. picture , if you will, a $400 steam loco not set up for top speed. Some cranks the throttle open, the loco screams off at 250 mph. It enters a curve, slams to the floor in a thousand pieces. I will check out the post as suggested and keep you informed.
Wow, trains have come a long, long way since my interest in the 50s and 60s.

Ron
 

westaust55

Moderator
Wow, trains have come a long, long way since my interest in the 50s and 60s.

Ron
Yes they certainly have.
While many folks and clubs still use the D.C. control for layouts the use of Digital Command Control (DCC) where control signals are superimposed into the D.C. Supply creating a square wave with a frequency in the range 5 kHz to 8 kHz has become very popular.

DCC Locomotive decoders have also advanced significantly over the past decade.
Early versions have low frequency PWM and buzzing would be heard from the motor.
Modern decoders still use PWM however the frequency is typically from 15 kHz to around 23 kHz (to my knowledge).

As pfroment indicates a non sound decoder will have from just a few Configuration Variables (CV's) to around 100.
I understand that some of the Sound decoders now available could have around 400 CV's.
Event stationary/accessory decoders may have from just a few through to 400 CV's to program.
 

alhoop

Member
Here is code for checking speed and counting axles using a Picaxe 14M2.
It uses two IR gates 2 feet apart and for speed it reads from a table indexed by
how long it takes to travel 2 feet and uses B4 to store the result. Not very accurate at high speeds as the Picaxe does integers only.
For you purposes just mark off four feet and time the locomotive with a stopwatch.
10 seconds = 44 scale miles/hour.
Al
PS: here is unit in action.

https://www.youtube.com/watch?v=7wbeHSekYFY


Code:
#Picaxe 14m2
setfreq m8
#terminal 9600
symbol et = b0
Symbol dir = b1
symbol cnt = b2
symbol tim = b3
symbol spd = b4



table 0,(0,218,109,73,55,44,36,31,27,24,22,20,18,17,16,15,14,13,12,11,11,10,9,8)
serout c.4,t9600_8,(cr)
pause 2000
serout c.4,T9600_8,("v18",cr)
pause 1000
serout c.4,t9600_8,("n7",cr)
pause 1000

Start:
low b.5    'disble display
setint %00000000,%00000000
cnt = 1
cont:
if pinc.2 = 1 then east
if pinc.1 = 0 then cont
west:
setfreq m4
time = 0
dir = 1
contw:
if pinc.2 <> 1 then contw
et = time
do:loop until pinc.2 = 0
setint %00000100,%00000100  ;set int on east
for b5 = 0 to 30
pause 100
next b5
goto calc
east:
setfreq m4
time = 0
dir = 0
conte:
if pinc.1 <> 1 then conte
et = time
do:loop until pinc.1 = 0
setint %00000010,%00000010  ;set int on west
for b5 = 0 to 30
pause 100
next b5
calc:
readtable b0,b4
setfreq m8 
high b.2   ;turn on activity LED
pause 100
pulsout b.3,100  ;reset display to 0
pause 1000
for b6 = 1 to b2
pulsout b.4,100 ; pulse clock line
next b6      ;next loop
high b.5  ;enable display
pause 1000
serout c.4,T9600_8,("s","total axles",#b2,cr)
gosub waitforready
low b.4
low b.5 
pulsout b.3,100  ;reset display to 0
pause 300
low b.4
for b7 = 1 to b4
pulsout b.4,100 ; pulse clock line
next b7     ;next loop
high b.5  ;enable display
pause 1000
serout c.4,T9600_8,("s","no defects",cr)
gosub waitforready
serout c.4,T9600_8,("s","Speed is ",cr)
gosub waitforready
pause 100
serout c.4,T9600_8,("s",#b4,cr)                                                                                                                                                                                                
gosub waitforready 
pause 300
low b.2
low b.4
goto start
waitforready:
serin c.3,T9600_8,(":")
pause 1000       
return
end

interrupt:
	inc cnt
	if dir = 0 then
	do:loop until pinc.1 = 0
	setint %00000010,%00000010
	else
	do:loop until pinc.2 = 0
	setint %00000100,%00000100 
  	endif
  	b5 = 0
	return
 
Last edited:

westaust55

Moderator
I have added more SYMBOL statements, more comments and more indenting to alhoop's code:
Code:
#Picaxe 14m2
#terminal 9600

setfreq m8

; define the variables usage
symbol et 	 = b0
Symbol directn   = b1		; direction 0 = east, 1 = west
symbol axlecnt   = b2
symbol tim 	 = b3
symbol speed     = b4
symbol countr    = b5        ; for the For Next loop counter/index



; define the IO useage
SYMBOL LED 	  = B.2    	; High to turn activity LED ON
SYMBOL Disp_Reset = B.3
SYMBOL Disp_Clk	  = B.4		; Display clock pulse line
SYMBOL Disp_En	  = B.5		; High to turn display ON

SYMBOL Speech	  = C.4		; serial data pin for speech
SYMBOL SP_Done	  = C.3		; speech module ready


; speed lookup table
table 0,(0,218,109,73,55,44,36,31,27,24,22,20,18,17,16,15,14,13,12,11,11,10,9,8)

Init:
;Initialise the speech module
	serout Speech,T9600_8,(cr)
	pause 2000
	serout Speech,T9600_8,("v18",cr)
	pause 1000
	serout Speech,T9600_8,("n7",cr)
	pause 1000

Start:
	low		Disp_En   			; disble display
	setint	%00000000,%00000000	; disable unterrupts
	axlecnt  =	1

cont:
	if pinc.2 = 1 then east
	if pinc.1 = 0 then cont

west:
	setfreq m4
	time	=	0
	directn =	1

contw:
	if pinc.2 <> 1 then contw		; wait until reach east end sensor
	et 	= 	time
	do:loop until pinc.2 = 0		; wait until east sensor cleared
	setint 	%00000100,%00000100  	; set int on east

	for countr = 0 to 30			; wait for 3000 ms / 3 sec			
		pause 1000                        ; each time a new axle is detected (using Interrupts)  the counter is cleared
	next countr                                ; if no new axle for 3 seconds then all done
	goto calc

east:
	setfreq m4
	time	=	0
	directn =	0

conte:
	if pinc.1 <> 1 then conte		; wait until reach west end sensor
	et 	= 	time
	do:loop until pinc.1 = 0		; wait until west sensor cleared
	setint %00000010,%00000010  		; set int on west
	for countr = 0 to 30			; wait for 3000 ms / 3 sec
		pause 100                         ; each time a new axle is detected (using Interrupts)  the counter is cleared
	next countr                               ; if no new axle for 3 seconds then all done

calc:
	readtable et, speed
	setfreq m8 
	high LED  					; turn on activity LED
	pause 100
	pulsout Disp_Reset,100  		; reset display to 0
	pause 1000
	for countr = 1 to axlecnt
		pulsout Disp_Clk,100 		; pulse clock line
	next countr      ;next loop
	high Disp_En				; enable display
	pause 1000

; send text to the speech synthesis module
	serout Speech,T9600_8,("s","total axles",#axlecnt,cr)
	gosub waitforready			; wait until speech is completed

	low Disp_Clk
	low Disp_En					; disable display 
	pulsout Disp_Reset,100  		; reset display to 0
	pause 300
	low Disp_Clk
	for countr = 1 to speed
		pulsout Disp_Clk,100 		; pulse clock line
	next countr     				; next loop

	high b.5  					; enable display
	pause 1000

; send text to the speech synthesis module
	serout Speech,T9600_8,("s","no defects",cr)
	gosub waitforready			; wait until speech is completed
	serout Speech,T9600_8,("s","Speed is ",cr)
	gosub waitforready			; wait until speech is completed
	pause 100
	serout Speech,T9600_8,("s",#speed,cr)                                                                                                                                                                                                
	gosub waitforready			; wait until speech is completed 
	pause 300
	low LED					; turn LED off
	low Disp_Clk				; turn off display
	goto start

waitforready:
	serin SP_Done,T9600_8,(":")		; wait until speech is completed
	pause 1000       
	return
	end

interrupt:
	inc axlecnt
	if directn = 0 then			; if direction is east
		do:loop until pinc.1 = 0
		setint %00000010,%00000010	; set int on west
	else						; Else direction is west
		do:loop until pinc.2 = 0
		setint %00000100,%00000100 	; set int on east
  	endif
 	countr = 0	                              ; reset the FOR NEXT index/counter each time an axle is detected 
	return
Hopefully this will make it easier for pfroment to understand.


EDIT: some edits to code based on advise by Alhoop at post 13
 
Last edited:

Dartmoor

Member
Absolutely incomprehensible for an european :p:p:p ISU
Adding to the confusion, "in the UK N-gauge is 1:148 scale, which gives 35.676 feet for a mile."
Model railway scales in the UK are usually referred to in mm/ft! Which in this case is a Scale of 2.0625 mm to 1 ft! (according to Wikipedia).
I model in 16mm/ft, or bigger, so I have more space to fit picaxes.
I doubt this actually helps anyone but could give Besqueut a smile? :)
 

alhoop

Member
Last edited:
Top