first try to write a little macro

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

first try to write a little macro

Postby CADdy » Sun Oct 08, 2017 2:56 am

Hi all,

my first post, my first problem :-)

I want to switch a waterpump on and off via a macro. It's no problem to do this manual with the following listing:
---------------------
Code: Select all
M20600.txt:
// Macro to switch waterpump off

 int Port;
 int Pin;

 Port = 1;
 Pin = 9;

exec.Setoutpin(Port,Pin);
exec.AddStatusmessage(" Waterpump turned OFF ");


---------------------
Code: Select all
M20601.txt:
// Macro to switch Waterpump on

int Port;
int Pin;

Port = 1;
Pin = 9;

exec.Clroutpin(Port,Pin);
exec.AddStatusmessage(" Waterpump turned ON ");

---------------------
In 2017 screenset I've created a button to switch the relais on and off:

screen1.png


The macro M20610.txt contains the following:
---------------------
Code: Select all
bool buttonstate = AS3.Getbuttonstate(20610);

if (buttonstate)
{
   AS3.Setcheckboxstate(false, 20601);
   AS3.Switchbutton(false,20610);
}
else
{
   AS3.Setcheckboxstate(true, 20600);
   AS3.Switchbutton(true,20610);

}

---------------------

Clicking on the new button toggles between on and off, shown with pictures "CheckBox_Btn_Up.png and CheckBox_Btn_Down.png". This function of the button ist what I want, but nothing happens. The relais is not switching. I don't know why. I assumed that the lines "AS3.Setcheckboxstate(false, 20601);" and "AS3.Setcheckboxstate(true, 20600);" be called alternately.

I think, it's a very simple error that I made. But I don't find it. It's my first try with UCCNC-macros. Please be lenient with my programming skills (and my poor english) ;-)

Best Regards
Peter
CADdy
 
Posts: 148
Joined: Sun Sep 17, 2017 2:26 pm

Re: first try to write a little macro

Postby dezsoe » Sun Oct 08, 2017 9:52 am

Hi Peter,

Just change your M20610.txt to:

Code: Select all
bool buttonstate = AS3.Getbuttonstate(20610);

if (buttonstate)
{
   exec.Code("M20601"); // Turn off pump
   AS3.Switchbutton(false,20610);
}
else
{
   exec.Code("M20600"); // Turn on pump
   AS3.Switchbutton(true,20610);
}
while (exec.IsMoving()){} // Wait for exec.Code to finish
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: first try to write a little macro

Postby ger21 » Sun Oct 08, 2017 10:54 am

Or, just use the M20610 for everything.
Code: Select all
bool buttonstate = AS3.Getbuttonstate(20610);
int Port=1;
int Pin=9;

if (buttonstate)
{
   exec.Setoutpin(Port,Pin);
   exec.AddStatusmessage(" Waterpump turned OFF ");
   AS3.Switchbutton(false,20610);
}
else
{
   exec.Clroutpin(Port,Pin);
   exec.AddStatusmessage(" Waterpump turned ON ");
   AS3.Switchbutton(true,20610);
}
while (exec.IsMoving()){} // Wait for exec.Code to finish
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2663
Joined: Sat Sep 03, 2016 2:17 am

Re: first try to write a little macro

Postby CADdy » Sun Oct 08, 2017 4:16 pm

Hi dezsoe, Hi Gerry,

thank you for your professional help! Both solutions work perfectly.
I think the last line (while (exec.IsMoving....)) was my problem. I didn't know this line was necessary.

BTW, I changed the position of the button to a nicer place ;-)

Thank you very much!
Peter
CADdy
 
Posts: 148
Joined: Sun Sep 17, 2017 2:26 pm


Return to Macros

Who is online

Users browsing this forum: No registered users and 9 guests