Hello Guest it is April 27, 2024, 09:41:12 AM

Author Topic: How do you make a screen button turn on and off a relay?  (Read 471 times)

0 Members and 1 Guest are viewing this topic.

How do you make a screen button turn on and off a relay?
« 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?

Offline thosj

*
  •  532 532
    • View Profile
Re: How do you make a screen button turn on and off a relay?
« Reply #1 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,
--
Tom
Re: How do you make a screen button turn on and off a relay?
« Reply #2 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
Re: How do you make a screen button turn on and off a relay?
« Reply #3 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

'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline thosj

*
  •  532 532
    • View Profile
Re: How do you make a screen button turn on and off a relay?
« Reply #4 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.
--
Tom

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: How do you make a screen button turn on and off a relay?
« Reply #5 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