Sub Main()ToolNumberDRO=824	'set the OEM Code 824 to a variableCapacity=8 		'tool holder capacitypos1=0			'initialize position variables to zeropos2=0pos3=0pos4=0pos5=0pos6=0pos7=0pos8=0count = 0 		'initialize count variables to zerodone  = 0		'initialize done variables to zeroOldTool=GetCurrentTool 	'get tool# that Mach3 thinks is in turret.  This is used only to maintain			'existing tool# In DRO-824 If macro exits before ending.NewTool=GetSelectedTool 'get the new commanded tool# from G-Code 'TX01M6'setOEMDRO(ToolNumberDRO,OldTool)	'set DRO 824 to the currently stored Mach3 tool#.					'NewTool only gets input if macro executes properly'-------'check to see if new tool# makes senseIf NewTool > Capacity Then	Message("Tool Number Too High, range 1-"&Capacity)	code "M00"	Exit Sub	End IfIf NewTool < 1 Then	Message("Tool Number Too low, range 1-"&Capacity)	code "M00"	Exit SubEnd If'-------'Everything seems to be OK so-far, get the current tool position in the turret directly from PLC'by polling the 8-positin reed switch. If a switch position is closed, that is the current toolpos1=GetInput(0)pos2=GetInput(1)pos3=GetInput(2)pos4=GetInput(3)pos5=GetInput(4)pos6=GetInput(5)pos7=GetInput(6)pos8=GetInput(7)'-------'set the current tool # based on which switch is closed If pos1=1 Then CurrentTool=1If pos2=1 Then CurrentTool=2If pos3=1 Then CurrentTool=3If pos4=1 Then CurrentTool=4If pos5=1 Then CurrentTool=5If pos6=1 Then CurrentTool=6If pos7=1 Then CurrentTool=7If pos8=1 Then CurrentTool=8'-------If pos1+pos2+pos3+pos4+pos5+pos6+pos7+pos8 <> 1 Then	Message("turret switch broken, or turret out of position, pausing program")	code "M00"	Exit SubEnd If'-------'check to see if new tool# = CurrentTool#.  If so, nothing needs to happen, exit.If NewTool = CurrentTool Then	Message("Old Tool = New Tool, Returning")	setOEMDRO(ToolNumberDRO,CurrentTool)		'This is here because we now know which tool the PLC							'thinks is in position, and that should be correct.	'code "M00"					'commented out 12-31-09, trying to fix jumping of servos		Exit Sub					End If'-------'Move to Machine coordinates -12-3code "G53 G0 Z-12"code "G53 G0 X-3"While IsMoving()Wend'-------'Figure out which direction to turn the turret for the shortest toolchangedifference=NewTool-CurrentTool			'Subtract the current tool from the new tool	If difference < 0 Then difference=difference+8	'If the difference is negative, then add it to 8 so the number is between 1-8direction = 8-difference			'This is a little hard to explain without a picture, but basically if you						'subtract old from new tool you get numbers from -7 to 7.  If you force all those to						'be positive by adding the negative ones to 8 (in other words if you get -3, add it to 8						'for a sum of 5) and then subtract that from 8, you get the numbers 1-7.  1,2,3 are one						'direction, and 7,6,5 are the other.  4 does not matter, same distance either way.xIf direction = 1 Then SetModOutput (0,0)	'sets motor direction clockwose facing -ZIf direction = 2 Then SetModOutput (0,0)	'sets motor direction clockwose facing -ZIf direction = 3 Then SetModOutput (0,0)	'sets motor direction clockwose facing -ZIf direction = 4 Then SetModOutput (0,0)	'sets motor direction clockwose facing -Z						'direction does not matter here, but might as well go the same way						If direction = 5 Then SetModOutput (0,1)	'sets motor direction counter clockwose facing -ZIf direction = 6 Then SetModOutput (0,1)	'sets motor direction counter clockwose facing -ZIf direction = 7 Then SetModOutput (0,1)	'sets motor direction counter clockwose facing -Z'-------'unlock turret'check to see if locked switch is functioningTurret_Clamped=GetInput(32)If Turret_Clamped <> 1 Then	Message("Turret Clamped Switch Malfunction, Returning and Pausing Program")						'If the turret is not fully camped or if switch is bad operation stops	code "M00"	Exit Sub					End IfDocount = count + 1SetModOutput(10,1)sleep 100done = GetInput(33)If done=1 Then Exit Do	If count > 50 Then		Message ("turret unclamp timed out, pausing program")		SetModOutput(10,0)		code "M00"		Exit Sub	End IfLoopdone=0'-------'Actually do the tool change, now that the direction relay is set and the turret is unlocked the PLC does not need to deal with that.If NewTool=1 Then	Do	count = count + 1	SetModOutput(1,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(1,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(1,0)	done=0	End If'-------If NewTool=2 Then	Do	count = count + 1	SetModOutput(2,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(2,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(2,0)	done=0	End If'-------If NewTool=3 Then	Do	count = count + 1	SetModOutput(3,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(3,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(3,0)	done=0	End If'-------	If NewTool=4 Then	Do	count = count + 1	SetModOutput(4,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(4,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(4,0)	done=0	End If'-------	If NewTool=5 Then	Do	count = count + 1	SetModOutput(5,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(5,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(5,0)	done=0	End If'-------If NewTool=6 Then	Do	count = count + 1	SetModOutput(6,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(6,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(6,0)	done=0	End If'-------	If NewTool=7 Then	Do	count = count + 1	SetModOutput(7,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(7,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(7,0)	done=0	End If'-------If NewTool=8 Then	Do	count = count + 1	SetModOutput(8,1)	sleep 100	done = GetInput(73)	If done=1 Then Exit Do		If count > 100 Then			Message ("tool change timed out, pausing program")			SetModOutput(8,0)			code "M00"			Exit Sub		End If	Loop	SetModOutput(8,0)	done=0	End If	setOEMDRO(ToolNumberDRO,NewTool)		'store the new tool in DRO 824'-------'lock turretcount = 0Docount = count + 1SetModOutput(10,0)sleep 100done = GetInput(32)If done=1 Then Exit Do	If count > 50 Then		Message ("turret clamp timed out, pausing program")		code "M00"		Exit Sub	End IfLoopdone=0sleep 500	End SubMain        