Thanks TPS, that workaround for the timer has worked really well. I have done some tests this morning and my macro now seems to be working as it should, which I'm really happy about!!
Something that almost always catches me out is when setting and reading I/O's or DRO's, a sleep is needed as Mach either misses the command, or crashes if not.
I've posted the code below so others might be able to make use of it:
'-----------------------------------------------------------------------
'CYCLONE - Auto tool changer (8 pos turret type)
'V1.0
'-----------------------------------------------------------------------
'-----------------------------------------------------------------------
'CONFIG
'-----------------------------------------------------------------------
Dim TS As Single
Dim TE As Single
Dim TT As Single
NewTool = GetSelectedTool() 'Commanded tool number
OldTool = GetCurrentTool() 'Current tool number
MaxToolNum = 8 'Max number of tools for the changer
PreviousSpeed = GetRPM() '
ToolSpeed = 500
'-----------------------------------------------------------------------
'MACRO START
'-----------------------------------------------------------------------
DeActivateSignal(OUTPUT6) 'Make sure signal to PLC is negative
SetSpinSpeed(ToolSpeed) 'Spindle speed change
code"G91 G28 X0" 'set incremental, return home via X first
While IsMoving()
Wend
code"G91 G28 Z0"
While ISMoving()
Wend
While NewTool > MaxToolNum 'Dont allow tool number greater 8
Message("MAX TOOL NUMBER = 8")
Wend
While NewTool < 0 'Dont allow tool number less than 1
Message("INVALID TOOL NUMBER")
Wend
If NewTool = OldTool Then
Call ToolComplete
Else
If IsActive(INPUT1) Then 'ok signal from PLC
Call DoToolChange
Else
code"M00"
code"M5"
Message("TURRET NOT READY!")
End If
End If
End
'----------------------------------------------------------------------
'TOOL CHANGE
'----------------------------------------------------------------------
Sub DoToolChange()
TS = Timer 'get the start time
SetCurrentTool(NewTool) 'Update the tool DRO
ActivateSignal(OUTPUT6) 'Set tool change allowed signal to PLC
sleep 500
While IsActive(INPUT1) = False 'While the turret is moving, count down
TE = Timer
If (TE - TS) > 4 Then
Call ToolFailed
End If
Wend
Call ToolComplete
End Sub
'----------------------------------------------------------------------
'TOOL COMPLETE
'----------------------------------------------------------------------
Sub ToolComplete()
DeActivateSignal(OUTPUT6)
sleep 100
TT = TE - TS
SetSpinSpeed(PreviousSpeed) 'RPM before tool change called
Message("Tool Change Complete in " & Round(TT,1) & " seconds")
sleep 100
End Sub
'----------------------------------------------------------------------
'TOOL FAILED
'----------------------------------------------------------------------
Sub ToolFailed()
DeActivateSignal(OUTPUT6)
sleep 100
Code"M00"
Code"M5"
Message("TURRET TIMEOUT TOOL " & NewTool & " NOT LOCATED" )
sleep 100