this is my code, may be someone need it, or you are able to make it better
Thanks
Massimo
FUNCTION_BLOCK FB_MovingAverage
VAR_INPUT
IN : REAL;
N_Cicli : INT;
t_Pausa_1 : DINT;
t_Pausa_2 : DINT;
END_VAR
VAR_OUTPUT
Media : REAL;
END_VAR
VAR
Valore : ARRAY [0..999] OF REAL;
x, x1, x2: INT;
SCHEDULER_1: OSCAT_BASIC.SCHEDULER;
A, B, C, D: REAL;
E: BOOL;
END_VAR
SCHEDULER_1(
E0:= TRUE,
E1:= TRUE,
T0:= DINT_TO_TIME(t_Pausa_1),
T1:= DINT_TO_TIME(t_Pausa_2));
IF SCHEDULER_1.Q0 THEN
A := A + IN;
B := B + 1;
END_IF
IF SCHEDULER_1.Q1 THEN //MEDIA DELLE LETTURE BREVI
C := A / B;
A := 0;
B := 0;
D := 0;
E := TRUE;
END_IF
IF E THEN
FOR x := 1 TO (N_Cicli-1) DO
x1 := N_Cicli - x;
x2 := N_Cicli - x - 1;
Valore[x1] := Valore[x2];
END_FOR
Valore[0] := C;
FOR x := 0 TO (N_Cicli-1) DO
D := D + Valore
END_FOR
E := FALSE;
Media := D / N_Cicli; //MEDIA TRASCINATA
END_IF