' Water Sampler Test Program
' Sept 18, 1998
'-------------------------------------------------------------------------
Start:

gosub initialize
gosub checkbs
start1:
gosub trigger
gosub sprocket
gosub ballscrew
goto start1
'-------------------------------------------------------------------------

trigger:		'Wait for Topside Trigger Pulse

btnWk     var	byte
btnWk=0
debug "Waiting for Trigger",cr
'trigger1
trig var word		'temp
for trig = 1 to 1000	'temp
Button 7,0,255,250,btnWk,1,exit
next
'Goto trigger1
exit:
debug "Pulse Received",cr
return
'--------------------------------------------------------------------------

checkbs:		'Test for Ball Screw Home Position

out9=1		'phase set to Fwd
out10=0		'enable set to on
out8=1		'brake set to off
pause 300	'Displace Beyond In Switch
debug "BS is retracting",cr
debug "Wait for In Switch",cr
out9=0		'phase set to Rev

checkbs1:
if in12=0 then brake
goto checkbs1

brake:
out8=0		'brake set to on
debug "In Switch is On",cr
debug "BS Brake is on",cr
pause 1000
return
'--------------------------------------------------------------------------

sprocket:		'Advance Sprocket One Position

debug "Advance Sprocket One Position",cr
out2=0		'enable set to on
out1=0		'phase set to rev?
out0=1		'brake set to off

pause 200	'get clear of switch
sprocket1:
if in4=0 then sbrake
goto sprocket1

sbrake:
out0=0		'brake set to on
debug "Syringe is Ready",cr
pause 1000
return
'--------------------------------------------------------------------------

ballscrew:		'Run Ball Screw one Flush & Suction Cycle

debug "BS is Extending",cr
debug "Wait for Out Switch",cr
out9=1		'phase set to fwd
out10=0		'enable set to on
out8=1		'brake set to off

ballscrew1:	'Test Out Switch
if in13=0 then Brakeout
goto ballscrew1

brakeout:
out8=0		'brake set to on
debug "Out Switch is ON",cr
debug "BS Brake On, Flush finished",cr

pause 1000

debug "BS is Retracting",cr
out9=0          'Phase set Rev
out10=0         'Enable set On
out8=1		'Brake set Off

ballscrew2:	'Test In Switch
if in12=0 then brakein
goto ballscrew2

brakein:
out8=0		'brake set to on
debug "BS Brake On, Suction finished",cr
return
'--------------------------------------------------------------------------

initialize:		'Setup Basic Stamp and Motor Controller

debug "Water Sampler Test Program",cr
' set pin directions
' sprocket pins
output 0		'Brake
output 1		'Phase
output 2		'Enable
output 3		'Mode
input  4 		'Hall Sensor
' Trigger pin
input 7		        'Start Pulse
' Ballscrew pins
output  8		'Brake
output  9		'Phase
output 10		'Enable
output 11		'Mode
input 12		'In Position Switch
input 13		'Out Position Switch
'Set Initial Conditions for Sprocket
out0=0			'Brake on
out1=0			'Phase set to Fwd
out2=1			'Enable set to off
out3=1			'Mode set to fast decay
in4=1			'Hall Sensor normally high
in7=1			'Start Pulse normally high
'Set Initial Conditions for Ballscrew
out8=0                  'Brake set On
out9=0                  'Phase set Forward
out10=1                 'Enable set Off
out11=1                 'Mode set Fast
return
'--------------------------------------------------------------------------
