function m6() local inst = mc.mcGetInstance() --Get the instance of Mach4 mc.mcCntlGcodeExecuteWait(inst, "g04 p500") mc.mcCntlCycleStart(inst) local currentTool = mc.mcToolGetCurrent(inst) --looks for the tool currently selected local selectedTool = mc.mcToolGetSelected(inst) -- looks at the tool you have selected local tpos = (selectedTool * 60) --Tool position is the current tool number * 60 (example tpos = (selected tool) Example tool 2 --> 2 * 60 = tpos = 120) local Out = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT21) --Get handle for output 22 and set to "out" variable --local currentTool = 1 --local selectedTool = 4 if (selectedTool ~= currentTool) then --Change tool: if the selected tool isnt = to the current tool, you want to change your tool local hReg, rc = mc.mcRegGetHandle(inst, "iRegs0/CurrentSlot") mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos + 8 ))) if (selectedTool > currentTool) then --mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos + 8 ))) mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f" , tpos)) --Reference, move to position, back up to lock position mc.mcCntlGcodeExecuteWait(inst, "g04 p1500") mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos - 7.74146 ))) rc = mc.mcRegSetValue(hReg, selectedTool) mc.mcToolSetCurrent(inst, selectedTool) elseif(selectedTool < currentTool) then mc.mcSignalSetState(Out, 1) --Turn OUTPUT21 on --mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos - 8 ))) mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f" , tpos)) --Reference, move to position, back up to lock position mc.mcSignalSetState(Out, 0) --Turn OUTPUT21 off mc.mcCntlGcodeExecuteWait(inst, "g04 p500") mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos - 7.74146 ))) rc = mc.mcRegSetValue(hReg, selectedTool) mc.mcToolSetCurrent(inst, selectedTool) end end --This function is here to allow the debugging from the Lua editor if (mc.mcInEditor() == 1) then m6() end end --end