Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)Dim A As Integer Dim X As Integer Dim VAR As IntegerDim OldTool As IntegerDim NewTool As IntegerDim Tool As IntegerDim ZCoord As IntegerSetSpinSpeed(200)SetOEMDRO(805,0)				'Set C-Axis DRO to 0Call ToolReadCall AlignSpindleCall MoveZCall TurnDirectionCall ToolSense(NewTool)Call EndRoutine'//// Subroutines /////////Sub ToolReadIf isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then Oldtool=1If Not isactive(Input1)And isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then Oldtool=2If isactive(Input1)And isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then Oldtool=3If Not isactive(Input1)And Not isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then Oldtool=4If isactive(Input1)And Not isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then Oldtool=5If Not isactive(Input1)And isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then Oldtool=6If isactive(Input1)And isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then Oldtool=7If Not isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then Oldtool=8If isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then Oldtool=9If Not isactive(Input1)And isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then Oldtool=10NewTool = GetSelectedTool() If NewTool <1 Then                  			  MsgBox ("Error! Tool "& NewTool & " Out Of Range")     DoOemButton (1003)                       		  End                                      		End IfIf NewTool >10 Then                  			  MsgBox ("Error! Tool "& NewTool & " Out Of Range")  DoOemButton (1003)                     End                                     End IfIf NewTool<>OldTool Then Exit SubIf NewTool = OldTool Then End End Sub'----------------------------------------------------------------------------'----------------------------------------------------------------------------Sub AlignSpindle'-------------------- If spindle RPM =0 and Aligned -------------------------If GetOemDro(39) <10 Then			 	  If isactive(INDEX) And isactive(OEMTRIG1) Then 	    ActivateSignal(OUTPUT9)			'Activate Locking Pin    Exit Sub  End IfEnd If '-------------------- If spindle RPM =0 and not Aligned ---------------------If GetOemDro(39) <10 Then								  '----M4--------				'Start spindle CCW to brake  DoSpinCCW() 					'CCW Relay  ActivateSignal(OUTPUT6)			'Spindle enable  '--------------   				  ActivateSignal(OUTPUT9)			'Activate Locking Pin  While Not isactive(INDEX) And Not isactive(OEMTRIG1)    sleep 10   Wend  '----M5--------				'Start spindle CCW to brake  deActivateSignal(OUTPUT6)			'Spindle disable  DoSpinStop() 					'Disable CCW Relay  '--------------   				End If '-------------------- If spindle RPM > 10 and not Aligned -------------------'--------------------------- Dynamic spindle braking ------------------------If GetOemDro(39) > 10 Then			'Get True spindle  '----M4--------				'Start spindle CCW to brake  DoSpinCCW() 					'Enable CCW Relay  ActivateSignal(OUTPUT6)			'Spindle enable  '--------------   				    While GetOemDro(39) > 100			'Check if spindle speed above 100rpm    sleep 1      If GetOemDro(39) > 100 Then       activateSignal(OUTPUT6)			'Activate Spindle ENABLE       sleep 50       deactivateSignal(OUTPUT6)		'Activate Spindle ENABLE      End If    Wend   ActivateSignal(OUTPUT9)			'Activate Locking Pin  '----M4--------				'Start spindle CCW to brake  DoSpinCCW() 					'Enable CCW Relay  ActivateSignal(OUTPUT6)			'Spindle enable  '--------------   				   While Not isactive(INDEX) And Not isactive(OEMTRIG1)    sleep 10   Wend  '----M5--------				'Start spindle CCW to brake  deActivateSignal(OUTPUT6)			'Spindle disable  DoSpinStop() 					'Disable CCW Relay  '--------------   				End If '---------------------- Double Check Spindle Index Alignment -----------------------If Not isactive(INDEX) And Not isactive(OEMTRIG1) Then MsgBox("Tool Alignment Fail")  End Sub '----------------------------------------------------------------------------'----------------------------------------------------------------------------Sub MoveZcode "G1 G53 Z130 F3000"		'Move Z-Axis to tool change positionWhile ismoving()WenddeActivateSignal(OUTPUT9)End Sub'----------------------------------------------------------------------------'----------------------------------------------------------------------------Sub TurnDirectionActivateSignal(OUTPUT11)		'Enable C-stepper driverIf OldTool < NewTool Then A= 1If OldTool > NewTool Then A= -1VAR= (OldTool + (5*A) - NewTool)If A= 1 ThenIf VAR <=0 Then    X= (OldTool + 10) - NewTool   Else   X= (OldTool - NewTool)   End IfEnd IfIf A= -1 ThenIf VAR <=0 Then    X= (OldTool - NewTool)   Else   X= (OldTool - 10 - NewTool)   End IfEnd IfX= X*36End Sub'----------------------------------------------------------------------------'----------------------------------------------------------------------------Sub ToolSense(ByVal CaseNumber As Integer)Tool = 0Select Case CaseNumber    Case Is = 1    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=1    	End If    Wend           Case Is = 2    	code "G91 C" & X & "F4500"	While Newtool <> Tool    	If Not isactive(Input1)And isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=2    	End If    Wend        Case Is = 3    	code "G91 C" & X & "F4500"	While Newtool <> Tool    	If isactive(Input1)And isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=3    	End If    Wend       Case Is = 4    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If Not isactive(Input1)And Not isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=4    	End If    Wend        Case Is = 5    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If isactive(Input1)And Not isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=5    	End If    Wend        Case Is = 6    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If Not isactive(Input1)And isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=6  	End If    Wend        Case Is = 7    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If isactive(Input1)And isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=7    	End If    Wend        Case Is = 8    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If Not isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then    	DoOEMButton(1003)    	Tool=8    	End If    Wend        Case Is = 9    	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then    	DoOEMButton(1003)    	Tool=9    	End If    Wend        Case Is = 10	code "G91 C" & X & "F4500"    	While Newtool <> Tool    	If Not isactive(Input1)And isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then    	DoOEMButton(1003)    	Tool=10    	End If    Wend    End Select If Tool = 0 Then MsgBox ("Tool Number not Correct") End Sub'----------------------------------------------------------------------------'----------------------------------------------------------------------------Sub EndRoutinecode"G1 G90 G53Z0 F4000"			'Move Z-Axis back to 0 posWhile ismovingWendSetOEMDRO(824,Tool)				'Set DRO with new Tool numberdeActivateSignal(OUTPUT11)			'Disable Turret Stepper DriverMessage ("Turret Station # " & tool)End Sub  doButton(0)					'Cycle StartEnd                                                                                               