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(500)SetOEMDRO(805,0)				'Set C-Axis DRO to 0Call ToolReadCall AlignSpindleCall MoveZCall TurnDirectionCall ToolSense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Do'-------------------- If spindle RPM =0 and Aligned -------------------------If GetOemDro(39) <10 Then			 	  If isactive(INDEX) And isactive(OEMTRIG1) Then 	    ActivateSignal(OUTPUT9)			'Activate Locking Pin    sleep 10    Exit Sub  End IfEnd If '-------------------- If spindle RPM =0 and not Aligned ---------------------If GetOemDro(39) <10 Then								  SetSpinSpeed(30)  ActivateSignal(OUTPUT9)			'Activate Locking Pin  '----M4--------				'Start spindle CCW to brake  DoSpinCCW() 					'CCW Relay  ActivateSignal(OUTPUT12)			'Spindle enable  '--------------   				  While Not isactive(INDEX) And Not isactive(OEMTRIG1)    sleep 1   Wend  '----M5--------				'Start spindle CCW to brake  deActivateSignal(OUTPUT12)			'Spindle disable  DoSpinStop() 					'Disable CCW Relay  '--------------   	  While ismoving   Wend			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(OUTPUT12)			'Spindle enable  '--------------   				    While GetOemDro(39) > 50			'Check if spindle speed above 100rpm    sleep 1      If GetOemDro(39) > 50 Then       activateSignal(OUTPUT12)			'Activate Spindle ENABLE       sleep 50       deactivateSignal(OUTPUT12)		'Activate Spindle ENABLE      End If    Wend    ActivateSignal(OUTPUT9)End If '--------------------------- Loop ------------------------If isactive(INDEX) And isactive(OEMTRIG1) Then   sleep 100  If isactive(INDEX) And isactive(OEMTRIG1) Then     ActivateSignal(OUTPUT9)    sleep 500    Exit Do  End IfEnd If LoopEnd Sub '----------------------------------------------------------------------------'----------------------------------------------------------------------------Sub MoveZcode "G1 G53 Z130 F4000"		'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If NewTool = 1 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=1    	End IfEnd If       If NewTool = 2 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If Not isactive(Input1)And isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=2    	End IfEnd If If NewTool = 3 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If isactive(Input1)And isactive(Input2)And Not isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=3    	End IfEnd If If NewTool = 4 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If Not isactive(Input1)And Not isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=4    	End IfEnd If If NewTool = 5 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If isactive(Input1)And Not isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=5    	End IfEnd If If NewTool = 6 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If Not isactive(Input1)And isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=6  	End IfEnd If If NewTool = 7 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If isactive(Input1)And isactive(Input2)And isactive(Input3)And Not isactive(Input4) Then    	DoOEMButton(1003)    	Tool=7    	End IfEnd If If NewTool = 8 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If Not isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then    	DoOEMButton(1003)    	Tool=8    	End IfEnd If If NewTool = 9 Then     	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If isactive(Input1)And Not isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then    	DoOEMButton(1003)    	Tool=9    	End IfEnd If If NewTool = 10 Then 	code "G91 C" & X & "F4500"    	While IsMoving()   	Wend   	Sleep 10    	If Not isactive(Input1)And isactive(Input2)And Not isactive(Input3)And isactive(Input4) Then    	DoOEMButton(1003)    	Tool=10    	End IfEnd If  If Tool = 0 Then   MsgBox ("Tool Number not Correct")   End End If 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)doButton(0)					'Cycle StartEnd Sub  End                                                                                                            