Jump to content

ST function FOR TO statement


HansCZ

Recommended Posts

Hi all,

maybe have a stupid question, but do not understand. I try to define a MovingAverage function in ST:

FUNCTION MovingAverage
    VAR_INPUT
        Values:    ARRAY[0..4] OF DINT;
        NewValue:    DINT;
    END_VAR
    VAR_OUTPUT
        Result:    DINT;
    END_VAR
    VAR
        Sum:    DINT;
    END_VAR
    (* User code starts below this comment *)
    FOR i := 4 TO 1 DO
       Values[i] := Values[i-1];
       Sum := Sum + Values[i];
    END_FOR;
    Values[0] := NewValue;
    Sum := Sum + NewValue;
    Result := Sum/5;
    (* User code ends above this comment *)
END_FUNCTION

But this does not work for me (the values are not changed. But if I use instead of  "FOR TO DO" primitive def like

Values[4] := Values[3];

Values[3] := Values[2];

.

.

 

etc. it works.

Can somebody explain it to me?

Thx. Jan

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.