Hello Guest it is November 08, 2024, 07:46:52 PM

Author Topic: Toggle button  (Read 5417 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Toggle button
« Reply #6 on: February 11, 2021, 03:57:52 PM »
Hello
I had a bit of a play, and having very limited script knowledge, I've developed the following script which uses the status of Output 1 LED to either toggle the spindle on or off.

'Spindle Toggle ON/OFF

If GetLED(Output1) <> 1 Then      'Is Output 1 enabled?
 DoSpinStop            'If YES Turn OFF Spindle
 DeactivateSignal(OUTPUT1)      'And Disable Output 1
End If
If GetLED(Output1) <> 0 Then      'Is Output 1 disabled?
 DoSpinCW            'If YES Turn ON Spindle
 ActivateSignal(OUTPUT1)      'And Enable Output 1
End If
Re: Toggle button
« Reply #7 on: February 11, 2021, 04:00:01 PM »
That looks like VB not lua :) and looks like Mach3 calls.
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: Toggle button
« Reply #8 on: February 11, 2021, 04:03:38 PM »
Right you are, didn't fully read the subject link.
Re: Toggle button
« Reply #9 on: February 11, 2021, 04:39:47 PM »
No problem man! Just wanted people to know when they saw it!
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com