' sampler test program
'set pin directions
' sprocket pins
output 0		'Brake
output 1		'Phase
output 2		'Enable
output 3		'Mode
input  4 		'Hall Sensor
'activate 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
'ballscrew
out8=0
out9=0
out10=1
out11=1
'Pulse from topside set high normally		
'Loop on start pulse to detect TTL
btnWk     var	byte
hall      var     byte
btnWk=0

Loop1:
 Button 7,0,255,250,btnWk,1,firstStep
debug "waiting"
Goto Loop1

firstStep:		'pulse from topside detected and sampler activated
debug "pulse recieved"
'check to make sure ballscrew is retracted
debug "ball screw is retracting"
out9=0			'phase set to reverse
out10=0			'enable set to on
out8=1			'brake set to off
'pause 100

Loop2:
if in12=0 then BrakeF
debug "waiting for out switch",CR
goto Loop2

BrakeF:
out8=0			'brake set to on
debug "state of pin 12:",bin out12,cr
debug "brake is on !!"
debug "Is ball screw retracted ?",CR
debug "If not, kill it ! Sprocket will turn in 5 seconds",CR
pause 5000

'goto next syringe
out2=0		'enable set to on
out0=1 		'brake set to off
debug "motor is turned on"
debug "state of pin 2:",bin out2,cr	
pause 200

Loop3:
 If in4=0 then Sbrake
 debug "waiting for brake",CR
goto Loop3

Sbrake:
debug "got to sprocket brake sensor"
out0=0 	'brake set to on

out2=1	'enable set to off
debug "Is syringe aligned ? You have 5 seconds to kill this if not!!!"
pause 5000
goto BallScrew

Loop4:
debug "waiting for next pulse"
If in7=0 then lastStep
goto Loop4

lastStep:
debug "next pulse recieved !"
goto Loop1

BallScrew:
out9=1		'phase set to fwd
out10=0		'enable set to on
out8=1		'brake set to off
goto step5

step5:
Loop5:
debug "ball screw is extending"
if in13=0 then Brakeout
goto Loop5
'pause 2000
'goto Brakeout:

Brakeout:
out8=0		'brake set to on
debug "got the out switch",CR


out9=0		'phase set to Reverse
debug "ball screw brake for flush is activated"
goto Pullin

Pullin:
debug "ball screw is retracting"
out10=0
out8=1		'brake set to off
'pause 500
goto Loop6

Loop6:
if in12=0 then Brakein
goto Loop6

Brakein:
out8=0		'brake set to on
debug "ball screw brake for suction activated"
goto Loop4
stop