SpinTool = GetOEMDRO (1200)'Tool in spindlePotTool = GetOEMDRO (1201) 'Tool in tool changerIf SpinTool <> PotTool Then   MsgBox "Spindle Tool and Pocket Tool are not the Same"   speak("Spindle Tool and Pocket Tool Are Not The Same, Error. Error")   DoOEMButton( 5 )   Exit SubEnd IfNewTool = GetSelectedTool() 'Programed toolIf NewTool = SpinTool Then   Exit SubEnd IfIf NewTool = 0 Then   MsgBox "No Tool Number Given"   speak("Tool Called Is 0. Error. Error")   DoOEMButton( 5 )   Exit SubEnd IfIf NewTool < 0 Then   MsgBox "Invalid Tool Number"   speak("Tool Called Is Less Then 1, Error. Error")   DoOEMButton( 5 )   Exit SubEnd IfIf NewTool > 16 Then   MsgBox "Invalid Tool Number"   speak("Tool Called Is More Then 16, Error. Error")   DoOEMButton( 5 )   Exit SubEnd IfDoSpinStop()        speak("Initiating Tool Change")        While IsMoving()        Sleep (10)        Wend        ActivateSignal(OUTPUT5)'Orient Spindle        Code "G01 G53 Z.038 F150." 'Home Z Axis        While IsMoving()        Sleep (10)        Wend        While IsActive(OEMTRIG12) = False 'Oreintation complete        Sleep(10)        Wend'Find shortest route to next toolChgTool = (NewTool - SpinTool)If ChgTool < 0 Then    ChgTool = (16 + ChgTool)End IfIf ChgTool > 0 And ChgTool <= 16 Then   Call CW(ChgTool)   Exit SubEnd IfIf ChgTool > 0 And ChgTool > 8 Then   ChgTool = (-16 + ChgTool) * -1   Call CCW(ChgTool)   Exit SubEnd If'Clockwise move to next toolSub CW(ByVal ChgTool As Integer)	                While IsActive(ZHOME) = False	Sleep(10)        Wend        If IsActive(ZHOME) = False Then	MsgBox "Z is not Home. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		If IsOutputActive(OUTPUT5) = False Then 'Oreint spindle	MsgBox "Spindle is not Orientated. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		ActivateSignal(OUTPUT9)'Magizine advance		While IsActive(OEMTRIG9) = False 'Magizine Forward limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG9) = False Then 'Magizine Forward limit sw	MsgBox "Mag is not Forward. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		ActivateSignal(OUTPUT8) 'Unclamp tool		While IsActive(OEMTRIG15) = False 'Tool unclamp limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG15) = False Then 'Tool unclamp limit sw	MsgBox "Tool is Clamped. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If			ActivateSignal(OUTPUT10) 'Magizine down		While IsActive(OEMTRIG11) = False 'Magizine down limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG11) = False Then 'Magizine down limit sw	MsgBox "Mag is not Down. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If  			Call IndexCW(ChgTool)	End Sub	'Counter Clockwise move to next toolSub CCW (ByVal ChgTool As Integer)	While IsActive(ZHOME) = False	Sleep(10)        Wend        If IsActive(ZHOME) = False Then	MsgBox "Z is not Home. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		If IsOutputActive(OUTPUT5) = False Then 'Oreint spindle	MsgBox "Spindle is not Orientated. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		ActivateSignal(OUTPUT9)'Magizine advance		While IsActive(OEMTRIG9) = False 'Magizine Forward limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG9) = False Then 'Magizine Forward limit sw	MsgBox "Mag is not Forward. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		ActivateSignal(OUTPUT8) 'Unclamp tool		While IsActive(OEMTRIG15) = False 'Tool unclamp limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG15) = False Then 'Tool unclamp limit sw	MsgBox "Tool is Clamped. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If			ActivateSignal(OUTPUT10) 'Magizine down		While IsActive(OEMTRIG11) = False 'Magizine down limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG11) = False Then 'Magizine down limit sw	MsgBox "Mag is not Down. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If  			Call IndexCCW(ChgTool)	End SubSub IndexCW(ByVal ChgTool As Integer)	 ' Enable Motor        ActivateSignal( OUTPUT11 )                    ' Decrement current position n Times            For x = 1 To ChgTool                    ' Make sure we clear the Index Switch            Timeout = 0            While IsActive( OEMTrig5 )                Sleep 10                                ' Check for TimeOut ( 3 Second )                Timeout = Timeout + 1                If Timeout = 300 Then                    Message( "ToolChanger Index Sw did not clear" )                    Exit Sub                                    End If            Wend                ' Make sure we see the Index Switch            Timeout = 0            While Not( IsActive( OEMTrig5 ))                Sleep 10                                ' Check for TimeOut ( 3 Second )                Timeout = Timeout + 1                If Timeout = 300 Then                    Message( "ToolChanger Index Sw was not detected" )                    Exit Sub                                    End If            Wend                                Next x    ' Stop the Motor    DeActivateSignal( OUTPUT11 )    Call ChangeToolEnd Sub		Sub IndexCCW (ByVal ChgTool As Integer)	' Enable Motor        ActivateSignal( OUTPUT12 )        ActivateSignal( OUTPUT11 )                    ' Decrement current position n Times            For x = 1 To ChgTool                    ' Make sure we clear the Index Switch            Timeout = 0            While IsActive( OEMTrig5 )                Sleep 10                                ' Check for TimeOut ( 3 Second )                Timeout = Timeout + 1                If Timeout = 300 Then                    Message( "ToolChanger Index Sw did not clear" )                    Exit Sub                                    End If            Wend                ' Make sure we see the Index Switch            Timeout = 0            While Not( IsActive( OEMTrig5 ))                Sleep 10                                ' Check for TimeOut ( 3 Second )                Timeout = Timeout + 1                If Timeout = 300 Then                    Message( "ToolChanger Index Sw was not detected" )                    Exit Sub                                    End If            Wend                          Next x    ' Stop the Motor    DeActivateSignal( OUTPUT12 )    DeActivateSignal( OUTPUT11 )    Call ChangeTool    End SubSub ChangeTool			If IsActive(OEMTRIG9) = False Then	MsgBox "Mag is not Forward. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		If IsActive(OEMTRIG15) = False Then	MsgBox "Tool is Clamped. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		DeactivateSignal(OUTPUT10) 'Magizine up	While IsActive(OEMTRIG10) = False 'Magaizine up limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG10) = False Then 'Magaizine up limit sw	MsgBox "Mag is not Up. Manually change tool"	Dobutton(5)	Exit Sub	End If		DeactivateSignal(OUTPUT8) 'Clamp tool	While IsActive(OEMTRIG14) = False 'Tool clamp limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG14) = False Then 'Tool clamp limit sw	MsgBox "Tool is not Clamped. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		DeactivateSignal(OUTPUT9) 'Magizine back	While IsActive(OEMTRIG8) = False 'Magizine back limit sw	Sleep(10)	Wend		If IsActive(OEMTRIG8) = False Then 'Magizine back limit sw	MsgBox "Mag is not Back. Manually change tool"	DoOEMButton( 5 )	Exit Sub	End If		DeactivateSignal(OUTPUT5) 'Oreintation off		'SetOEMDRO (2003, 0)	SetOEMDRO (1200,GetSelectedTool())	SetOEMDRO (1201,GetSelectedTool())	SetCurrentTool( GetSelectedTool )				speak("Tool Change Complete")	        Exit Sub  End Sub         