Structured text for loop decrement

g.mccormick

Lifetime Supporting Member
Join Date
Jul 2012
Location
IN
Posts
961
In structured text, can you decrement in a for loop?
I need to shift an array and I can either use while loop or if decrement in for loop works id do that.

Will this work?

VAR
index: INT;
my_arr: ARRAY[1..12] OF REAL;
END_VAR;

FOR index := 12 TO 2 BY -1 DO
my_arr[index] := my_arr[index-1];
END_FOR;
 
yes it does work, however try to remove all numbers from the program and put the in the VAR list, this way you are sure you have the correct types
and negative USINT is not working good.
this array is small and good to do however if it is getting bigger a pointer is faster.
 
Shooter. What do you mean pointer is faster? I need this code to run very quickly, I've set the program to 1ms execution time.
 
here you have to shift every var 12 times, however if you set an index you can direct point to the correct value, lets suppose you have 12 in an array. The last position is 5 and you have 8 values in your index.
so the pointer is 5 and your number is 8.
now you can start to find the values by use the pointer in the array index and the number as next one. keep in mind that you have to loop at 12.
This way you do not shift, but change the starting position in the array
for more info look at www.oscat.de (
 
I still do not really understand the pointer or why it is needed. I have changed my code to get rid of all but one for loop. I do use a for loop to zero out the array when calculations should be no allowed.
Now I am just incrementing an index variable and using it to put the values into array positions. I have a flag for if the array is full or not, if it is not full, I change the value I divide the summation by.
I will post some code later tonight, but the just is something like:
calc_OSF(CLK:= calculation_enable);

if calc_OSF.Q then
for index:=1 to max_arr_index do
arr[index]:=0;
end_for;
b_arr_if_full := FALSE;
END_if;


outflow_value := arr[arr_index];
arr[arr_index]:= raw_torq;

summation:= (summation - outflow_value) + arr[arr_index];

if b_arr_is_full then
average := summation / max_arr_index;
else
average := summation / arr_index;
end_if;

if arr_index < max_arr_index then
arr_index := arr_index +1;
else
arr_index:= 1;
b_arr_is_full := TRUE;
end_if;
 

Similar Topics

Hey all, Studio v20.05 Background. We are piggy backing another system to record attributes for product travelling down the line and several...
Replies
21
Views
3,509
I am trying to learn structured text on the controllogix platform and have managed to fault the processor by creating an infinite loop. My problem...
Replies
21
Views
3,724
Hi guys, I have a basic problem...when the start relay is activated...I want my derived function block to load a set of values to an array...
Replies
8
Views
2,876
Hello Everyone: I have been able to read value via canopen interface using ADS protocol. Actually my Structured Text Programming is weak. I can...
Replies
5
Views
3,984
I have to write some rslogix 5000 code that will loop through an array, copy each element in the array one at a time to a separate tag and set a...
Replies
19
Views
31,380
Back
Top Bottom