I see what you're trying to do, but there is a better way.
Mach 4 offers the ability to respond to a change of state of a signal. This happens in the background and doesn't require a user script to be looping to check for it. It acts something like this: "If I see a change in state of a button, run this function".
So rather than looping, looking for your "OK to run" input, you can check the signal state before you start (perhaps as part of the cycle start button) and then just respond to a change in state.
Let's look at what this might look like in code. First off, we need to place the function call. I have done this in the Screen Load Script, but it can also be done in the Signals Script.
SigLib = {
[mc.ISIG_INPUT5] = function (state) --Change the input number
SystemOKChange() --This is called if there is a change with the input state
end
}
This code will call a function SystemOKChange() whenever INPUT5 (change this to what you need) changes state.
Next we need to create this function (put it at the end of your screen load script). The is called when the script changes state, but we don't yet know what the new state is. To get this, we need the handle (pointer to memory address of that input signal) and then query Mach 4 for the signal state
function SystemOKChange()
local hSysOK
local SysOK
hSysOK, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5) --Change Input #
SysOK, rc = mc.mcSignalGetState(hSysOK)
if (SysOK == 0) then --Invert this logic if needed here
mc.mcCntlEStop(inst) --Activate the EStop signal in Mach
mc.mcCntlSetLastError(inst, "System OK Input Lost: ESTOP Active") --Show a message in the Screen Set
else
mc.mcCntlSetLastError(inst, "System OK!") --Show a message in the Screen Set
end
end
Now with this code, Mach isn't spending time in a loop looking for a change in your input. Because of this, all of your on-screen controls should continue to work fine. You can expand on that SigLib{} list to add additional inputs.
Finally, we don't want your Cycle Start Button to work if the input is OFF. To do this, we remove the property binding from the cycle start button to CycleStart() and add our own button pressed script.
local inst = mc.mcGetInstance()
local hSysOK
local SysOK
hSysOK, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5) --Change Input #
SysOK, rc = mc.mcSignalGetState(hSysOK)
if (SysOK == 1) then --Invert this logic if needed here
mc.mcCntlCycleStart(inst)
else
mc.mcCntlSetLastError(inst, "Cannot Start, System OK Input is OFF") --Show a message in the Screen Set
end