txrocks
I am making this way harder than I need to.
The plan is to have an input bool to increment and decrement T_SP
 
The first time through I want T_SP to be set at 70 degrees and not be able to be turned higher than 80 or lower than 50.  
It is for the temperature input to my HVAC system. The way it is, it will keep resetting T_SP back to 0.  
 
I suck at ST. IT seems like it is going back to the default initial values of the variables on each pass through.
 
Any tips? Or I am on another planet with the logic here ? Thanks!
 
VAR_INPUT
  T_UP : bool;
  T_DN : bool;
END_VAR

VAR_OUTPUT
  T_SP : int;
END_VAR

VAR
  UpPrev : bool;
  DnPrev : bool;
  FirstScan : bool;
  T_min : int := 50;
  T_max : int := 80;
  T_SP_last : int;
  T_Initial : int := 70;
END_VAR



IF FirstScan THEN
    T_SP := T_Initial;
    T_SP_last := T_SP;                 (* Set initial value to 70 on first scan *)
    FirstScan := TRUE;           (* Only run this once *)

END_IF;

(* Increment on rising edge of T_UP *)
IF T_UP AND NOT UpPrev THEN
    IF T_SP < T_max THEN
        T_SP := T_SP + 1;
    END_IF;
END_IF;

(* Decrement on rising edge of T_DN *)
IF T_DN AND NOT DnPrev THEN
    IF T_SP < T_min THEN
        T_SP := T_SP - 1;
    END_IF;
END_IF;

(* Update previous states for edge detection on next scan *)
UpPrev := T_UP;
DnPrev := T_DN;
T_SP_last := T_SP;
 
Reply 0 0
thiagoralves
No need to have all your initial value logic. Simply set T_SP initial value to 70 on the variable declaration itself, like you did with T_Initial. That’s it! Also, make sure you’re creating a Function Block, not a Function
Reply 1 0
Reply