Hello Guest it is November 14, 2019, 10:17:32 PM

Author Topic: Toggle button  (Read 855 times)

0 Members and 1 Guest are viewing this topic.

Toggle button
« on: May 01, 2018, 07:52:40 AM »
I have built a panel with several buttons and a PoKey56E. I want two buttons to be a toggle for spindle on/off in forward and reverse. I used this code in the signal script.

Code: [Select]
[mc.ISIG_INPUT7] = function (state)
   if (state == 1) then   
       mc.mcCntlSetLastError(inst, "Spindle Forward Off");
       mc.mcSpindleSetDirection(inst, 0);
    else
        mc.mcCntlSetLastError(inst, "Spindle Forward ");
        mc.mcSpindleSetDirection(inst, 1);     
    end
end,

Problem is when the button is pressed the signal is called and the function turns on, but as soon as the button is released the signal script sees the change and call the function again, turning the spindle off.

Any idea how to handle this? Is there a way to only see the signal when it rises, not on any change?
Re: Toggle button
« Reply #1 on: May 01, 2018, 08:14:59 AM »
This is what I'm currently using on a machine here in the shop.  I haven't look at it in a while, this script can probably be updated.  But it is working like you want.  Push on/Push off.

------------Input 3----------------
--------Toggle SpinCW()-----------
[mc.ISIG_INPUT3] = function (state)
if (state == 1) then
     local inst = mc.mcGetInstance();
     local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
     local sigState = mc.mcSignalGetState(sigh);
     if (sigState == 1) then
         mc.mcSpindleSetDirection(inst, 0);
     else
         SpinCW()
     end
end
end,
Chad Byrd
Re: Toggle button
« Reply #2 on: May 01, 2018, 01:31:12 PM »
Hi,
a toggle button has two states and therefore requires two scripts.
One script will cause the spindle to start and the other will stop it.
As an example have a look at the Enable/Disable button in Mach4 wx4 screen set.

Craig
My wife left with my best friend...
     and I miss him!
Re: Toggle button
« Reply #3 on: May 01, 2018, 02:14:24 PM »
Thank you Cbyrdtopper that works perfectly. I also did the equivalent for reverse.

That was the last problem on my new control I have built for a friend to replace a dead Centroid.
Re: Toggle button
« Reply #4 on: May 01, 2018, 02:31:01 PM »
Aweseome! Glad that worked out for you then!
Chad Byrd
Re: Toggle button
« Reply #5 on: May 02, 2018, 07:48:17 AM »
Hi,
I have a little code that does things a little differently.
First I set up two toggle buttons, one ON/OFF and one FWD/REV. Note there is no need to put them in the signal library, they can be attached direct to an input signal,
I chose ISIG_INPUT10 and ISIG_INPUT11 respectively and these can in turn be assigned to a pin which is hooked to your external toggle switches.

There a four fragments of code, an 'up' script and a 'down' script for each button. I arbitrarily chose OSIG_OUTPUT4 for spindleON/spindleOFF and OSIG_OUTPUT5 for FWD/REV

ON/OFF up script:
Code: [Select]
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT4)
mc.mcSignalSetState(hsig,1)

ON/OFF down script:
Code: [Select]
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT4)
mc.mcSignalSetState(hsig,0)

FWD/REV up script:
Code: [Select]
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT5)
mc.mcSignalSetState(hsig,1)

FWD/REV down script:
Code: [Select]
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT5)
mc.mcSignalSetState(hsig,0)

Achieves the same result but uses the properties of the screen toggle button. Note if you want to be super code efficient you can attach an output directly to a toggle button so you don't even
have to write any scripts. I know.....where's the fun in that!

Craig
My wife left with my best friend...
     and I miss him!