Hi, 
This is basically the same code than before, excepted that I use Functions rather than subs. It's a little bit better, I can have Z moves but nothing more.
Normally the OUTPUT1 should be activated but there's no actions, MsgBox confirms the statement.
Something strange occurs, my call sequence isn't respected, I can check that using more MsgBox.
If I try to use the functions inside the Main Sub, this is not working at all: Syntax error, this is also strange to me.
Thanks for your attention,
Jerome
Sub Main()
'Global variables declaration
NewTool = GetSelectedTool()
OldTool = GetCurrentTool()
Dim Z_TcPos As Integer
Dim ZUp_TcPos As Integer
'Flow sequence
' Compare between actual tool and requested tool
If NewTool = OldTool Then
   End
   Else
End If
ActivateSignal(OUTPUT1)
'Carrousel air cylinder position check : Home, in Mvt and ToolChange
Call AirCyHome
MsgBox "Carrousel air cylinder position check"
'Spindle Home Sequence
Call SpindleHome
MsgBox "Spindle Home call"
'Spindle: Tool Clamp
Call Clamp
'Sequence to determine the least travel to move for TC, CW or CCW
Call LeastTravel
'Turn CW or turn CCW integrated in above Sub
'Activate air cylinder valve to put old tool back to carrousel
Call OldToolBack2TC
'Spindle: Tool Unclamp
Call Unclamp
'Move to Z tool position and wait until position reached
Call Z_TcPosition
'Spindle: Tool Clamp
Call Clamp
'Air Cylinder Homing
Call AirCyHome
'Store new tool as actual tool value
Call SetUserDRO (1200,NewTool)
SetCurrentTool( NewTool )
End Sub
'Carrousel air cylinder position check : Home, in Mvt and ToolChange
Function ToolChangerPos()
   If IsActive(INPUT1)>IsActive(INPUT2)Then 'We are at Home Position
   ToolChangerPos = 0
   TCMove = False 'We are stopped
   ElseIf IsActive(INPUT1)<IsActive(INPUT2) Then
      ToolChangerPos = 1
      TCMove = False
      
      ToolChangerPos = -1
      TCMove = True
   End If
End Function
'Air Cylinder Homing
Function AirCyHome() As Integer
   DeactivateSignal(OUTPUT8)
   AirCyHome=IsActive(INPUT1)
End Function
'Spindle Home Sequence
Function SpindleHome()
   ActivateSignal(OUTPUT1)'activate spindle indexing for ToolChange
   While(IsMoving())'Do nothing while spindle is rotating
      sleep(100)
   Wend
   'Maybe necessary to invoke a case select for tooling choice if more than 10 tools
'Show the user the state of the OUTPUT #1 input
If IsActive(OUTPUT1) Then
MsgBox "OUTPUT #1 output is active"
Else
MsgBox "OUTPUT #1 output is inactive"
End If
   Call ToolChangerPos
'Move to Z tool position and wait until position reached
   Call ZUp_TcPosition
'Activate air cylinder valve to put old tool back to carrousel
   Call OldToolBack2TC
'Spindle: Tool Unclamp
   Call Unclamp
'Go to safe tool change Z coordinates
   Call ZUp_TcPosition
End Function
'Move to Z tool position and wait until position reached
Function Z_TcPosition() 
   code("G53")'Move in Absolute Machine coordinates
   While(IsMoving())
      Sleep(100)'temporisation entre while++
   Wend
   code("G00 Z-116") 'Z value as to be checked for exact match with carrousel height
   While(IsMoving())
      Sleep(100)'temporisation entre while++
   Wend
   Z_TcPos=1 '1 quand en position pour Tool Change
End Function
'Spindle Home Sequence
'Spindle Release
Function SpindleRelease()
   DeactivateSignal(OUTPUT1)
End Function
'Put old tool back to carrousel
'Maybe necessary to invoke a case select for tooling choice if more than 10 tools
Function OldToolBack2TC ()
   If Z_TcPos=1 Then
      ActivateSignal(OUTPUT8)'Move carrousel via the air cylinder to reach the tool change position
   else
      Call Z_TcPosition
   End if
End Function
 
'Spindle: Tool Unclamp
Function Unclamp()
   If IsActive(INPUT2) then'Sensor indicates that carrousel is at tool change position then
       ActivateSignal(OUTPUT9)'Release the tool, unclamp
   End If
End Function
'Spindle: Tool Clamp
Function Clamp()
   DeactivateSignal(OUTPUT9)'Grip the tool, Clamp
End Function
'Go to safe tool change Z coordinates
Function ZUp_TcPosition()
   ZUp_TcPos=0 'var init
   code("G53") 'Move in absolute machine coordinates
   While(IsMoving())
      Sleep(100)
   Wend   
   code("G00 Z-35")'Move to the safe Z position for tool change
   while(IsMoving()) 
      sleep(100) '0.1sec delay between loops
   wend
   ZUp_TcPos=1
End Function
'Sequence to determine the least travel to move for TC, CW or CCW
'atp = Actual tool position, rtp = Requested tool positon
Function LeastTravel()
   NTool = 10 ' Number of tools changer holds
   CWPos = GetSelectedTool() - GetCurrentTool() ' Assume a CW move
   If CWPos < 0 Then ' CWPos < 0 ==> rtp < atp
      CWPos = CWPos + NTools
   End If
   CCWPos = NTools - CWPos
   If CWPos < CCWPos Then
      call CW()
   Else
      call CCW()
   End If
End Function
'Rotation CW
Function CW()
   dim x as Integer
   dim CTPos as Integer 'Carrousel Tool Position Relative to OldTool Position
   CTPos=0
   ActivateSignal(OUTPUT10)'Turns the AC Motor On
   Do until IsActive(OEMTrig1) 
      Sleep(50)
   loop
      for x=0 to CWPos
         If IsActive(OEMTrig1) then
            x=x+1
            While IsActive(OEMTrig1)
               'Wait for OEMTrig to de-assert
            Wend
         End If
      next x
      CTPos=x
   DeactivateSignal(OUTPUT10)
End Function
'Rotation CCW
Function CCW()
   dim y as Integer
   dim CTPos as Integer 'Carrousel Tool Position Relative to OldTool Position
   CTPos=0
   ActivateSignal(OUTPUT11)
   Do until IsActive(OEMTrig1) 'Turns the AC Motor On
      Sleep(50)
   loop
   for y=0 to CCWPos
      If IsActive(OEMTrig1) then
      y=y+1
         While IsActive(OEMTrig1)
            'Wait for OEMTrig to de-assert
         Wend   
      End If            
   next y
   CTPos=y
   DeactivateSignal(OUTPUT11)
End Function
Main