Author Topic: Increment / Decrement a variable  (Read 14959 times)

pstiles

  • Newbie
  • Posts: 12
    • View Profile
Increment / Decrement a variable
« on: October 27, 2006, 05:19:22 PM »
I am trying to increment and or decrement a variable. The variable is the desired temperature. The problem I am having is when I execute the following CusFN, the variable is always reset. How can I set a global variable?

A=72
B=A
If testio(Increase) then A=A+1
else
If testio(decrease) then A=A-1
endif
endif
setlcd 1,1,STR$(a)

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:Increment / Decrement a variable
« Reply #1 on: October 27, 2006, 09:17:43 PM »
What do you mean by the variable is reset? What is the value of A after you execute?

How often do you execute this custom function? Are you using a clock pulse to trigger this custom function? Is this function a differentiated version {dCusF}?

A better way for you to write this code would be to let "Increase" input trigger a {dCusF} where you will increase A, and "Decrease" input trigger another {dCusF} where you will decrease A. This way each time the "Increase" or "Decrease" input in triggered A will only be incremented/decremented once.

You can see an example in the "samples" folder called "SetTimCtr_SV.PC5" that uses push button to set the timers and counters SVs.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

pstiles

  • Newbie
  • Posts: 12
    • View Profile
Re:Increment / Decrement a variable
« Reply #2 on: October 28, 2006, 09:43:49 AM »
What I am attempting to do is use a HMI and plc for a thermostat. I have the HMI that is sold be Triangle Reasearch. I want to use one button to increase the temperature, and another button to decrease the temperature. Run I run the code in a CusFN, and I set variable b=72, I can increase the value of variable B, but when the code returne to to top, the variable is reset.

Thanks

Dogface

  • Newbie
  • Posts: 33
  • Woof$
    • View Profile
Re:Increment / Decrement a variable
« Reply #3 on: October 28, 2006, 11:38:41 PM »
I think I see the problem.  Try initiating the variable A=72 one time at the beginning of the program in a separate custom function.  Make sure that the custom function that makes the adjustment is a differentiated function, and you should have no problem.  Right now A is reset to 72 every time your function is called.

Hope this helps.
Dogface

pstiles

  • Newbie
  • Posts: 12
    • View Profile
Re:Increment / Decrement a variable
« Reply #4 on: October 30, 2006, 05:00:46 PM »
After hours spent trying to put all the parts together, I have failed. I can get the variable to increment / decrement, and turn the correct relay on and off based on the temp. The problem I am encountering is when I try to incorporate this code into my existing program that allows me to select either Heating, Cooling, Ventilation or System off, the variables to not increment. Can someone post a small program that I can follow to understand where to place the code to get the desired affect?

Thanks

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:Increment / Decrement a variable
« Reply #5 on: October 30, 2006, 05:22:06 PM »
Your problem is more likely some bugs in your program code rather than the snipplet of code that does not work.

If the variable does not increment, then the function that contains it is probably not executed. The best way to debug is to put some PAUSE statement at the place where the data is supposed to change. If the PLC never PAUSE it means that the code is not executed. If it pauses, you can check to see that the variable has changed, and it could be somewhere else that the variable has been changed back.

Email: support@triplc.com
Tel: 1-877-TRI-PLCS