Help with very basic macro - declaring a bool

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

Help with very basic macro - declaring a bool

Postby MattTheNoob » Sat Sep 05, 2020 7:47 pm

In this macro loop below, I can't seem to declare the boolean "spindleFaultEnunciated" without setting it to a value... but setting it to a value every time the loop runs isn't what I want. The way it is shown below throws a "Use of unassigned local variable 'spindleFaultEnunciated' error.

If I change the declaration to "bool spindleFaultEnunciated = false;" or true, I don't get an error, but it messes up what I'm trying to do. I want to Add the status message just one time... not every time the loop cycles.

I know this is really basic and I'm doing something foolish. I promise to smack myself in the head when you point out my mistake. ;)

Code: Select all
//   Macro execution starts here
   bool spindleFaultEnunciated;

   if (AS3.GetLED(349))         //Modbus Word 3, Bit 1, Spindle Fault from PLC
   {
      if (spindleFaultEnunciated == false)
      {
         exec.AddStatusmessage("Main Spindle VFD Fault");
         spindleFaultEnunciated = true;
      }
   }
      else
   {
      spindleFaultEnunciated = false;
   }


If I try to declare it in line with "If (bool spindleFaultEnunciated == false)", then I get this mess of errors.
CS1525 | in line: 36 | error text: Invalid expression term 'bool'
CS1002 | in line: 36 | error text: ; expected
CS1525 | in line: 36 | error text: Invalid expression term ')'
CS1002 | in line: 36 | error text: ; expected
MattTheNoob
Bostomatic 405 with updated controls... now entering Rev.2
UC300eth-5lpt, ModBus talking to Click PLC, Arduino Gecko servo monitor
Total UC noob.
MattTheNoob
 
Posts: 52
Joined: Sat Nov 03, 2018 3:36 pm

Re: Help with very basic macro - declaring a bool

Postby dezsoe » Sat Sep 05, 2020 10:35 pm

You have to declare your variables that you need in every cycle in the #Events section as static. See this topic.
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Help with very basic macro - declaring a bool

Postby MattTheNoob » Sun Sep 06, 2020 3:18 pm

That does the trick!! Thank you very much!

Code: Select all
//   Macro execution starts here


   if (AS3.GetLED(349))         //Modbus Word 3, Bit 1, Spindle Fault from PLC
   {
      if (spindleFaultEnunciated == false)
      {
         exec.AddStatusmessage(DateTime.Now.ToString("HH:mm:ss tt")+" Main Spindle VFD Fault");
         spindleFaultEnunciated = true;
      }
      if (AS3.GetLED(217)==false)         //check if Feed Hold is active
      {
         AS3.Switchbutton(true, 523);   //turn on feed hold
      }
   }
      else
   {
      spindleFaultEnunciated = false;
   }
Thread.Sleep(100);

#Events
static bool spindleFaultEnunciated=false;

Attachments
Status Message.png
MattTheNoob
Bostomatic 405 with updated controls... now entering Rev.2
UC300eth-5lpt, ModBus talking to Click PLC, Arduino Gecko servo monitor
Total UC noob.
MattTheNoob
 
Posts: 52
Joined: Sat Nov 03, 2018 3:36 pm


Return to Macros

Who is online

Users browsing this forum: No registered users and 8 guests

cron