#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