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 variable 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'-------'This makes sure that the turret is actually in a valid position'if no switch is closed or if multiple switches are closed there is a problem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"	Exit Sub					End If'-------'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.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'-------'Actually do the tool change, now that the direction relay is set the PLC does not need to deal with that.If NewTool=1 Then								'Check to see which tool is being called	Do	count = count + 1							'Add one to count.  This routine polls the "done" signal from the PLC every 100msec.											SetModOutput(1,1)							'initiate tool change routine for selected tool	sleep 100								'Let run 100msec	done = GetInput(73)							'Checkt to see if PLC is done	If done=1 Then Exit Do							'If it is done, quit waiting		If count > 100 Then						'Check to see if more than 10 sec, if not, keep waiting.  Should take 5 sec for longest change			Message ("tool change timed out, pausing program")	'If timeout, send message			SetModOutput(1,0)					'if timeout, turn off PLC routine			code "M00"						'If timeout, pause G-code			Exit Sub		End If	Loop	SetModOutput(1,0)							'if exiting loop, reset PLC routine off	(not sure I need to do this?)	done=0									'Reset done to zero (not sure I need to do this?)	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'I may add in here a little section to re-read the reed switches and verify the tool position just as another safety feature			setOEMDRO(ToolNumberDRO,CurrentTool)		'store the new tool in DRO 824 now that everything is done and proper	sleep 500					'For some reason I need this for back-to-back tool changes to work properly?End SubMain     