Need help with a macro

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Re: Need help with a macro

Postby dezsoe » Wed Nov 08, 2017 1:48 pm

Now the macro is ready to use. First you have to install the SwLED plugin. This will enable you to turn LEDs on/off even in reset state.

You need a button on the screen, like:

Code: Select all
AS3.Addbutton(x, y, w, h, true, false, buttonimagenumber, 5000 + yourLEDnumber, layernumber);

Set relayPort, relayPin and buttonLED in the following code as you need.

And this is the macro, which you have to run (and enable autorun) in macroloops:

Code: Select all
// ================================================================================================
// Switch output in reset state
// ================================================================================================

// Use UCCNC_SwLED plugin to handle switchbuttons. See http://www.forum.cncdrive.com/viewtopic.php?f=14&t=856

bool buttonState = exec.GetLED(buttonLED);

if (relayLED == -1)
  relayLED = GetLEDNumber(relayPort, relayPin);

if (exec.GetLED(relayLED) != buttonState)
{
  if (buttonState)
    exec.Setoutpin(relayPort, relayPin);
  else
    exec.Clroutpin(relayPort, relayPin);
}

// ================================================================================================

#Events

// ================================================================================================

const int relayPort = 2;                                                        // Port number of relay
const int relayPin = 2;                                                         // Pin number of relay

const int buttonLED = 471;                                                      // LED to store required state

static int relayLED = -1;
static int switchButton = 5000 + buttonLED;

// use AS3.Addbutton(x, y, w, h, true, false, buttonimagenumber, 5000 + yourLEDnumber, layernumber);

// =============== LED number based on port and pin

int GetLEDNumber(int port, int pin)
{
  int LED = -1;
  switch (port)
  {
    case 1:
      LED = pin;
      break;
    case 2:
      LED = 68 + pin;
      break;
    case 3:
      LED = 85 + pin;
      break;
    case 4:
      LED = 102 + pin;
      break;
    case 5:
      LED = 119 + pin;
      break;
  }
  return LED;
}
dezsoe
 
Posts: 2079
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Need help with a macro

Postby Dan911 » Wed Nov 08, 2017 2:20 pm

Hi dezsoe,
Do you see a problem just adding macro to button event in plugin, it seems much simpler and no macro loop needed.

Dan
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

Re: Need help with a macro

Postby dezsoe » Wed Nov 08, 2017 2:38 pm

Hi Dan,

Yes, it's true, but the question was a macro. Also, anyone can learn macro/macroloop technique to handle special events and outputs in reset state. The linked plugin also uses button event to turn switch and LED on/off. And in this macro the user can change the parameters any time, just needs to restart. I have no problems with macroloops, by default I have 7 running with no processor load, even on slower computers. So, why not macroloop? :)
dezsoe
 
Posts: 2079
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Need help with a macro

Postby dezsoe » Wed Nov 08, 2017 2:40 pm

And also, I always try to write universal codes with reuseable parts and easy to change parameters.
dezsoe
 
Posts: 2079
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Need help with a macro

Postby Dan911 » Wed Nov 08, 2017 2:53 pm

dezsoe wrote:Hi Dan,

Yes, it's true, but the question was a macro.


OK...Thanks for reply. But your method still involves a plugin for it to work. lol That's that great thing with UCCNC there's many paths to choose from.

Read post and responded quickly, a little more thought and probably even simpler method would be just use button event to call already written plugin.

Hey that's a great idea for a new plugin!

Dan
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

Re: Need help with a macro

Postby dezsoe » Wed Nov 08, 2017 3:02 pm

Dan911 wrote:But your method still involves a plugin for it to work. lol

It's true, but this plugin is also universal. Enable and forget it. :) If there is a button with number 5001..5000+MaxLED, it will handle it. I use some of this kind of buttons, because I can change settings without the need of pressing reset, and later in other macros or macroloops I can test the LED state.
dezsoe
 
Posts: 2079
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Need help with a macro

Postby Dan911 » Wed Nov 08, 2017 4:36 pm

1JUMPER10 wrote:Hi all. I'm trying to use a button and a Macro to activate a relay on Port 1, Pin 1. I used a macro that Ger21 had written and posted on this forum for another member and modified it for my purposes and made a button to call it. It worked as expected. The problem was my expectations were incorrect. The function of the relay is to be an override for a safety circuit so I can recover from a Reset condition. Like I said, in normal operation it works as expected, closing the relay on the first push, releasing it on the second. The problem is that it needs to work when the software is in Reset. UCCNC wont allow me to push the button when its in Reset. Here is the text of the macro:

bool buttonstate = AS3.Getbuttonstate(20001);
int Port=1;
int Pin=1;

if (buttonstate)
{
exec.Setoutpin(Port,Pin);
exec.AddStatusmessage(" OSSD NORMAL");
AS3.Switchbutton(false,20001);
}
else
{
exec.Clroutpin(Port,Pin);
exec.AddStatusmessage(" OSSD OVERRIDDEN");
AS3.Switchbutton(true,20001);
}
while (exec.IsMoving()){} // Wait for exec.Code to finish

Is it possible to make this work while in Reset?


Hi 1JUMPER10,

After testing plugin I posted to make sure its working correctly, using your macro I'm not sure it works the way you expect.

When button press its true and sets bool buttonstate to true.

So... when you first press button it sets it back to false(AS3.Switchbutton(false,20001);) and 1 pin led stays off.

Pin 1 led won't enable until the second time button press.

bool buttonstate = AS3.Getbuttonstate(20001); can be true or false at anytime.

I'm not sure what your trying to achieve and maybe that's way I'm confused.
Dan911
 
Posts: 613
Joined: Mon Oct 31, 2016 1:22 am
Location: USA

Re: Need help with a macro

Postby 1JUMPER10 » Wed Nov 08, 2017 5:31 pm

Dezsoe and Dan911 - Thank you for your work. I'll test both methods and report back.
1JUMPER10
 
Posts: 76
Joined: Tue Oct 04, 2016 1:54 am

Re: Need help with a macro

Postby 1JUMPER10 » Wed Nov 08, 2017 8:37 pm

Dezsoe -

I got the plug in installed but I'm a little confused about the button and the LED number: If I read this correctly, the LED number I need is the LED for Port 1, Pin 1 which is LED1. Is this correct? Or do I create a new LED and use that number?

AS3.Addbutton(x, y, w, h, true, false, buttonimagenumber, 5000 + yourLEDnumber, layernumber);

My button code would look like this:
AS3.Addbutton(x, y, w, h, true, false, 63, 5001, 1)

I made this macro M5001 per your code :

// ================================================================================================
// Switch output in reset state
// ================================================================================================

// Use UCCNC_SwLED plugin to handle switchbuttons. See viewtopic.php?f=14&t=856

bool buttonState = exec.GetLED(buttonLED);

if (relayLED == -1)
relayLED = GetLEDNumber(relayPort, relayPin);

if (exec.GetLED(relayLED) != buttonState)
{
if (buttonState)
exec.Setoutpin(relayPort, relayPin);
else
exec.Clroutpin(relayPort, relayPin);
}

// ================================================================================================

#Events

// ================================================================================================

const int relayPort = 1; // Port number of relay
const int relayPin = 1; // Pin number of relay

const int buttonLED = 1; // LED to store required state

static int relayLED = -1;
static int switchButton = 5000 + buttonLED;

// use AS3.Addbutton(x, y, w, h, true, false, buttonimagenumber, 5000 + yourLEDnumber, layernumber);

// =============== LED number based on port and pin

int GetLEDNumber(int port, int pin)
{
int LED = -1;
switch (port)
{
case 1:
LED = pin;
break;
case 2:
LED = 68 + pin;
break;
case 3:
LED = 85 + pin;
break;
case 4:
LED = 102 + pin;
break;
case 5:
LED = 119 + pin;
break;
}
return LED;
}

I configured the macro loop to run and to Auto run.

Is this all correct? I just dont understand what I'm doing yet. Coding and Macro's are brand new to me so I'm sure this all looks like stupid questions. :?
1JUMPER10
 
Posts: 76
Joined: Tue Oct 04, 2016 1:54 am

Re: Need help with a macro

Postby dezsoe » Wed Nov 08, 2017 10:29 pm

1JUMPER10,

No, don't use any built-in LED numbers. In version 1.2046 LED 255 is the last used one. I think, if you use LED 400, then you'll have no problem in later versions too.
dezsoe
 
Posts: 2079
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

PreviousNext

Return to Macros

Who is online

Users browsing this forum: No registered users and 9 guests