Title: Store variable value in each PLC cycle
Tags: plc;st
Question: Is it possible to store a variable value in each PLC cycle? I need the first 10 values each time to perform some calculations. I am using the OpenPCS platform and ST for programming.


Here is another answer: You can create an array of values and then store as array values
aBuffer : ARRAY[1..32] OF WORD;
init:BOOL; (* Init array *)
rest:BOOL; (*  Reset *)
val:WORD; (*  Value *)
iCount:INT; (* Array index *)
END_VAR
VAR_TEMP
iTmp : INT;
END_VAR
iTmp := UINT_TO_INT(N) - 1;
IF NOT init OR rest THEN
init := TRUE;
FOR iCount := 1 TO iTmp DO
aBuffer[iCount] := val;
END_FOR;
END_IF
iCount := INC1(iCount, 32);
aBuffer[iCount] := val;
This is a code example that created 32 elements array and every new PLC cycle assign new element and rotates.
After that, you can calculate the average or min and max.
INC1 increments given value by one until it reaches 32 and then reset to 1.
Comment for this answer: Hi @Sergey Romanov! Thank you for quick help. I have a problem declaring **VAR_TEMP** even though I am using separate FB.
Comment for this answer: It does not necessarily have to be temp. You can declare it in VAR scope.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:04/16/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<plc><st>', 'Title': 'Store variable value in each PLC cycle', 'ViewCount': 240, 'id': 61244217, 'user_AboutMe': None, 'user_AccountId': 14970856.0, 'user_DisplayName': 'Akshay Gaurihar', 'user_Reputation': 1.0, 'user_Views': 4.0, 'user_id': 10807782.0}}