New macro help - pulsing output

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

New macro help - pulsing output

Postby spumco » Fri Jun 25, 2021 4:21 am

I could use some help creating a function and I believe I need a macroloop to get it to work the way I want. I am still barely literate in macro language, despite everyone's assistance over the years here.

I'm attempting to create a pulsing (on, wait, off, wait, repeat) output on a particular pin for an air 'blast' system. I created something similar using a simple timer relay on a different machine and it was extremely useful in clearing chips out of deep pockets - even more so than flood coolant or a continuous air nozzle. This time I'd rather use a software solution.

The solenoid valve is currently activated by a relay via Port 3, Pin 2. So far I can can get the pin to cycle on & off, and the timing is adjustable from within the macro:

(M2002)
exec.Setoutpin(3,2);
exec.Wait (1000);
exec.Clroutpin (3,2);
exec.Wait (1000);


If I call the macro number via MDI or assign a button number it runs once. If I add it to the macroloop it will run continuously (oviously) with no way to turn it off except theough the macroloop configuration window.

What I want is to be able to activate/deactivate it using an M-code and/or a physical input pin.

So...
1. How do I get the macro to monitor a button state and then loop the above function? Ideally this would be a toggle-type function, as I have a single NO push button that is not assigned to another function.

2. Is it possible to map this pulsing loop to the M8 (flood) function? That would be really nice as my post processor would take care of enabling it ('flood cooling' for pockets), and would simplify the pushbutton and on-screen button assignment. The router doesn't have flood coolant, so there's no loss of function if M8 is turned in to an air blast thing.

Thanks in advance for any guidance,

-Ralph
spumco
 
Posts: 306
Joined: Mon Oct 03, 2016 10:10 pm

Re: New macro help - pulsing output

Postby dezsoe » Fri Jun 25, 2021 7:50 am

Hello Ralph,

Try the following macro as a macroloop. Set the port, pin, alow, onTime and offTime as you need. I didn't comment too much, I had only some minutes, but I hope it's enough.

Code: Select all
bool M8 = exec.GetLED(53);

if (M8 != lastM8)
{
  if (M8)
    state = 1;
  else
    state = 4;
  lastM8 = M8;
}

switch (state)
{
  // state 0: M8 is off, nothing to do

  case 1:
    // start/restart timer, turn on output
    count50ms = 0;
    turnOn();
    ++state;
    break;

  case 2:
    // output is on, watch onTime
    if (++count50ms >= onTime)
    {
      turnOff();
      count50ms = 0;
      ++state;
    }
    break;

  case 3:
    // output is off, watch offTime
    if (++count50ms >= offTime)
      state = 1;
    break;

  case 4:
    // turn off output, switch to idle state (0)
    turnOff();
    state = 0;
    break;
}

#Events

// output port, pin, active low
const int port = 3;
const int pin = 17;
const bool alow = false;

// time constants in 1/20 seconds (20 = 1s)
const int onTime = 10;
const int offTime = 20;

// variables
static int count50ms = 0;
static bool lastM8 = true;
static int state = 0;

// output functions

void turnOn()
{
  if (alow)
    exec.Clroutpin(port, pin);
  else
    exec.Setoutpin(port, pin);
}

void turnOff()
{
  if (alow)
    exec.Setoutpin(port, pin);
  else
    exec.Clroutpin(port, pin);
}
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: New macro help - pulsing output

Postby spumco » Fri Jun 25, 2021 11:38 am

Thanks Dezsoe! I'll have a go with it this evening and report back.

-Ralph
spumco
 
Posts: 306
Joined: Mon Oct 03, 2016 10:10 pm

Re: New macro help - pulsing output

Postby spumco » Sat Jun 26, 2021 12:02 am

PERFECT!

Thanks so much, the air blast pulse is working like a champ.
spumco
 
Posts: 306
Joined: Mon Oct 03, 2016 10:10 pm


Return to Macros

Who is online

Users browsing this forum: No registered users and 1 guest