I use beckhof twincat 3 with structured text.
I state that I don't know whether to sample every moment or sample every 50ms, but to be able to average all these samples, for example, could it be enough to do it every 50ms?
My code:
Wait(in:=true,pt:=t#50ms);
For I:=0 to b do
If wait.Q then
Arr[I] := L1_Voltage;
End_if
I:=I+1;
End_for
Wait is a tof, b length array and Arr is an array of REAL
Ah, @Rvaughan called it: there is a fundamental understanding how the PLC scan cycle works.
There is also something OP needs to understand about programming computers: a computer, or a PLC, does not care a whit what you
want it to do, but it will
always do
exactly what you
tell it to do. When it does something you do not want it to do, you can look at its behavior, knowing it is doing exactly what you told it, and use that to understan why it did that something, and that can lead to figuring out how to tell it to do what you want it to do.
Specifically, when I see a "problem statement" like the one in this thread:
The problem is that I create an array and a Tof timer that when the Q output is active takes the value and assigns it to the position of the current array through a for loop. The result is that in the array I have the first measured value in all positions
I say to myself, that is
not the problem; that PLC is doing exactly what OP asked it to do. The problem is that the OP wants it to do something else, something other than what they told it to do.
But enough of philosophy ...
The variable [
I] will increment from
0 to [
b]
on each scan.; it will
not take 50ms and several scans for [
I] to increment over that range.
So,
on each scan, the (typically) single value for
L1_Voltage will be assigned to
all the elements of Arr, from Arr[0] to Arr[
b].
Furthermore,
Wait.Q will always be True, on every scan; it will never be false.
Suggestions
- Watch those first few videos, even though they use Ladder, because they explain the scan cycle very well, and that is where part of the misunderstanding is.
- For the TOF, read the instruction documentation at this link.
- The important part to understand is this:
When IN is TRUE, Q is TRUE and ET is 0
- The timing diagrams at that link should also be illuminating.
- Consider, until you understand, what the name of the TOF instruction means: Timer OFf-delay
- Look at the documentation for the TON instruction as well.
- In the code posted by the OP, the IN argument to the TOF function is always assigned a value of TRUE, so the wait.Q value will alway be a value of 1 (TRUE)
- Ask The Google to search for examples of using the TOF and TON function.
- When you post your ST code, put [/ladder] after it, and
before it, so that the indentation is preserved.
[*]Once you understand how the scan cycle works and how the TOF function works,
- Write down in detail how you want the process to work e.g.
- On the first scan at the beginning of a period of 50ms, assign 0 to the value of I, to point to the first element of Arr
- On that first scan, and all subsequent scans until the 50ms elapses,
- Assign the value of L1_Voltage to the value of array element Arr
[*]Increment the value of I by 1, to point to the next element of Arr for the next scan
[*]On the first scan after the 50ms have elapsed,
- Do something with the values in the array.
- Prepare to start another period of 50ms on the next scan
[*]Once you have an acceptable description of how the process should work, you can start to write the code to make the PLC model the process, using the programming tools availableL
- := assignment;
- + operator;
- IF statements;
- TOF/TON functions;
- etc.