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 release = ((currentTool * 60) + 2.0)
   --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")
      
      if (selectedTool > currentTool) then
         mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos + 2.0 )))
         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 p500")
         mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos - .5 )))
         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 - 30 )))
            mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G1 F3000 A%4.4f", ( release )))
            mc.mcCntlGcodeExecuteWait(inst, "g04 p1000")
            mc.mcSignalSetState(Out, 1) --Turn OUTPUT21 on
            mc.mcCntlGcodeExecuteWait(inst, "g04 p1000")
            --mc.mcSignalWait(inst, out, WAIT_MODE_HIGH, 2.);  -- we waitup to 2 seconds for input 21 to go high
         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 p1000")
         mc.mcSignalSetState(Out, 0) --Turn OUTPUT21 off
         mc.mcCntlGcodeExecuteWait(inst, string.format("G53 G0 \nG53 G1 F3000 A%4.4f", ( tpos - .5)))
         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