Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: NOUSE94 on January 19, 2023, 12:35:57 AM

Title: How do you make a screen button turn on and off a relay?
Post by: NOUSE94 on January 19, 2023, 12:35:57 AM
I want to have a button operate a relay that uses an output # say output 25.  what code and what location to do this?
Title: Re: How do you make a screen button turn on and off a relay?
Post by: thosj on January 19, 2023, 08:51:14 AM
In the signal library section of the screen script, you can use an INPUT for the button, then have it trigger your output.

Here's an example of a button triggering the coolant pump relay. You'll just have to figure out how to trigger YOUR OUTPUT.

----------Mach4 Input 17 PoKeys Pin 17-----
----------Toggle Coolant M8----------------
[mc.ISIG_INPUT17] = function (state)
if (state == 1) then
     local inst = mc.mcGetInstance();
     local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_COOLANTON);
     local sigState = mc.mcSignalGetState(sigh);
     if (sigState == 0) then
         local OSigCool = mc.mcSignalGetHandle (inst,mc.OSIG_COOLANTON)
         mc.mcSignalSetState(OSigCool,1)
         mc.mcCntlSetLastError(inst, "Coolant On")
     else
         local OSigCool = mc.mcSignalGetHandle (inst,mc.OSIG_COOLANTON)
         mc.mcSignalSetState(OSigCool,0)
         mc.mcCntlSetLastError(inst, "Coolant Off")
     end
end
   
end,
Title: Re: How do you make a screen button turn on and off a relay?
Post by: NOUSE94 on January 19, 2023, 06:28:11 PM
In the signal library section of the screen script, you can use an INPUT for the button, then have it trigger your output.

Here's an example of a button triggering the coolant pump relay. You'll just have to figure out how to trigger YOUR OUTPUT.

----------Mach4 Input 17 PoKeys Pin 17-----
----------Toggle Coolant M8----------------
[mc.ISIG_INPUT17] = function (state)
if (state == 1) then
     local inst = mc.mcGetInstance();
     local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_COOLANTON);
     local sigState = mc.mcSignalGetState(sigh);
     if (sigState == 0) then
         local OSigCool = mc.mcSignalGetHandle (inst,mc.OSIG_COOLANTON)
         mc.mcSignalSetState(OSigCool,1)
         mc.mcCntlSetLastError(inst, "Coolant On")
     else
         local OSigCool = mc.mcSignalGetHandle (inst,mc.OSIG_COOLANTON)
         mc.mcSignalSetState(OSigCool,0)
         mc.mcCntlSetLastError(inst, "Coolant Off")
     end
end
   
end,

OK so somehow I got the button to work but i would like to have my spindle on button also trigger the coolant button or output signal
Title: Re: How do you make a screen button turn on and off a relay?
Post by: joeaverage on January 19, 2023, 07:08:50 PM
Hi,

Quote
OK so somehow I got the button to work but i would like to have my spindle on button also trigger the coolant button or output signal

In most cases, even if you use an on-screen button or an external button or use regular Gcode to turn your spindle on Mach uses an m3() function to perform the required task.
m3(), being such a ubiquitous function is already programmed into Mach4, and by-in-large its not a good idea to alter it. What you can do instead is write your
own m3() script.

When Mach encounters an m3() call it searches in the first instance to the macros folder of your profile. If it finds it then Mach will run the macro. If it does
not find it it will start searching 'up' the file tree until it finds an m3() macro. Your existing profile probably does not have an m3() and so Mach searches until it finds
one, namely the built-in m3().

If you write your own m3() and put it in the macros folder however Mach will use yours instead. You'd most likely want to put everything the the OEM m3() has in it
but then you can add whatever else you would like, for instance a couple of line of code to turn on the coolant pump.

Craig

Title: Re: How do you make a screen button turn on and off a relay?
Post by: thosj on January 20, 2023, 08:25:49 AM
Personally, I'd want a separate button for Coolant On and Spindle On, for those times you want one or the other but not both, but that's perhaps just me!! For me, there are a lot of times I want the spindle on without the coolant. Really, that's the reason for the buttons, otherwise the gcode turns them on when I really want them both on.

I guess you could always combine both in one button action, too.
Title: Re: How do you make a screen button turn on and off a relay?
Post by: smurph on January 21, 2023, 01:23:29 AM
I want to have a button operate a relay that uses an output # say output 25.  what code and what location to do this?
No coding is required.  To simply power a relay from a button, all you need to do is map the desired plugin output to the Mach output signal #25.  Then edit the screen, and place a toggle button anywhere you like.  Then in the toggle button's properties, drop down the output signal property and choose Output Signal #25.  Save the screen and exit out of the screen editor and your button should be controlling the plugin desire output.

Steve