this is the final
'Global variables declaration
 Global CCWPos As Integer
 Global CWPos As Integer
Sub Main()
NewTool = GetSelectedTool()
OldTool = GetCurrentTool()
While NewTool > 16
NewTool = Question ("Enter New Tool Number up to " & MaxToolNum)
Wend
If NewTool = OldTool Then'Compare between actual tool and requested tool
   Else
   End If
If NewTool > 16 Then'requested tool higher then tool numbers
   Else
   End If
If NewTool < 1 Then'tool number to low
   Else
   End If
code("m05")
   Call ZatcPosition
   Call SpidleOrient'Activate drive spindle orient
   Call AtcIn'Activate air cylinder valve to put old tool back to carrousel
   Call Unclamp'Spindle: Tool Unclamp
   Call AtcDown'lowers atc
   Call LeastTravel'determine the least travel to move and Turns CW or CCW
   Call AtcUp'raises atc
   Call Clamp'Spindle: Tool Clamp
   Call AtcOut'retracts atc
   Call SetUserDRO (1200,NewTool)'Store new tool as actual tool value
SetCurrentTool( NewTool )
End Sub
Function ZatcPosition()'Move to Z tool position and wait until position reached
   code("G53")'Move in Absolute Machine coordinates
   code("G00 Z-1") 'Z value as to be checked for exact match with carrousel height
   While(IsMoving())
      Sleep(100)'temporisation entre while++
   Wend
End Function
Function SpidleOrient()'Spindle Orientation
      ActivateSignal(OUTPUT1)'Move spindle to position output# 
      ActivateSignal(OUTPUT8)'Move spindle to position output# 
      code("g04 p01")
           While IsActive(OEMTrig11)'Wait for OemTrig11 is off spindle drive oreient ready
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig11)'Wait for OemTrig11 is on spindle drive output
      Sleep(10)
      Wend
End Function
Function AtcIn()'Put old tool back to carrousel
      ActivateSignal(OUTPUT12)'Move carrousel air cylinder to the tool change position adjust output# to match atc slide
      code("g04 p01")
           While IsActive(OEMTrig3)'Wait for OEMTrig3 is off Mag Out
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig3)'Wait for OEMTrig3 is on carrousel in
      Sleep(10)
      Wend
End Function
Function Unclamp()'Spindle: Tool Unclamp
       ActivateSignal(OUTPUT13)'Release the tool, adjust output number for drawbar output
       ActivateSignal(OUTPUT14)'Clean tool
code("g04 p01")'pause
           While IsActive(OEMTrig8)'Wait for OEMTrig8 is off Unclamp
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig8)'Wait for OEMTrig8 is on Unclamp
      Sleep(10)
      Wend
End Function
Function AtcDown()'lowers atc to rotate
   ActivateSignal(OUTPUT11)'solonoid to lower atc
   code("G04 p01")'pause
           While IsActive(OEMTrig6)'Wait for OEMTrig6 is off Mag down
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig6)'Wait for OEMTrig6 is on Mag down
      Sleep(10)
      Wend
End Function
'Sequence to determine the least travel to move for TC, CW or CCW
'atp = Actual tool position, rtp = Requested tool positon
Sub LeastTravel()
   NewTool = GetSelectedTool()
   OldTool = GetCurrentTool()
   NTool =16 ' Number of tools changer holds
   CWPos = GetSelectedTool() - GetCurrentTool() ' Assume a CW move
   If CWPos < 0 Then ' CWPos < 0 ==> rtp < atp
      CWPos = CWPos + NTool
   End If
   CCWPos = NTool - CWPos
   If CWPos < CCWPos Then
      Call CW()
   Else
      Call CCW()
   End If
End Sub
Function CW()'Rotation CW
   Dim x As Integer
   Dim CTPos As Integer 'Carrousel Tool Position Relative to OldTool Position
   CTPos=0
   ActivateSignal(OUTPUT9)'Turns the AC Motor On cw
   ActivateSignal(OUTPUT10)'Turns the AC Motor On
   Sleep(50)
   For x=1 To CWPos
           While IsActive(INPUT4)'Wait for INPUT4 is off 
      Sleep(10)
      Wend
           While Not IsActive(INPUT4)'Wait for INPUT4 is on 
      Sleep(10)
      Wend
        Next x
    CTPos=x
   DeactivateSignal(OUTPUT9)
   Deactivatesignal(OUTPUT10)'atc motor revirsing relay
End Function
Function CCW()'Rotation CCW
   Dim y As Integer
   Dim CTPos As Integer 'Carrousel Tool Position Relative to OldTool Position
   CTPos=0
   Activatesignal(OUTPUT9)'atc motor revirsing relay
    
    Sleep(50)
    
     For y=1 To CCWPos
           While IsActive(INPUT4)'Wait for INPUT4 is off 
      Sleep(10)
      Wend
           While Not IsActive(INPUT4)'Wait for INPUT4 is on 
      Sleep(10)
      Wend
        Next y
   CTPos=y
   DeactivateSignal(OUTPUT9)
   Deactivatesignal(OUTPUT10)'atc motor revirsing relay
End Function
Function AtcUp()'MAG UP
   DeActivateSignal(OUTPUT11)'relay for solonoid to lower/MAG UP
   code("G04 p01")'pause
           While IsActive(OEMTrig5)'Wait for OEMTrig5 is off Mag up
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig5)'Wait for OEMTrig5 is on Mag up
      Sleep(10)
      Wend
End Function
Function Clamp()'power drawbar unactivated
   DeactivateSignal(OUTPUT13)'unactivating drawbar relay
   DeactivateSignal(OUTPUT14)'unactivating Clean tool
           While IsActive(OEMTrig7)'Wait for OEMTrig7 is off Clamp
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig7)'Wait for OEMTrig7 is on Clamp
      Sleep(10)
      Wend
End Function
Function AtcOut()'retracts atc
      deActivateSignal(OUTPUT12)'atc retract cylinder relay Mag Inn
      code("g04 p01")
           While IsActive(OEMTrig4)'Wait for OEMTrig4 is off Mag Inn
      Sleep(10)
      Wend
           While Not IsActive(OEMTrig4)'Wait for OEMTrig4 is on Mag Inn
      Sleep(10)
      deActivateSignal(OUTPUT1)'Spindle Orientation
      deActivateSignal(OUTPUT8)'Spindle Orientation
     
   Wend  
End Function