Well done! You have discovered that
time is the most important part of PLC programming, and also that PLC scan cycle is the clock.
The PLC scan cycle is the loop you are looking for; there may be as many as one thousand scan cycles occurring over each single second of time. You want your code to only store the value only on one scan cycle every second, and to do nothing on all other scan cycles during that second. To do that it will need to detect an event or status that changes exactly once every second. There are many ways to do this; which is best depends on several factors:
- How accurately in time does this once-per-second event need to be measured?
- What is the make, model, and environment of the PLC that will be running this code (Siemens, Allen-Bradley, Codesys, Structured Text, Ladder Logic, etc.)?
- After 100 seconds, 100 locations in the array will have values, and presumably the 100-element array will be full; where do you want to put the 101st (101.) value?
- Is the FOR loop shifting the values in the array to make room for the updated value?
- What programming experience do you have besides PLCs?
Possible solutions:
- Put the code, that stores one updated value into the array, into a timed interrupt task that executes once every second (every 1000ms).
- This approach requires you to configure the PLC to run that task once per second
- the details of how that is done depends on the make, model, and coding environment of the PLC.
- The timing accuracy is probably best in this approach.
- Use an event that occurs each time the stored integer value is updated, and detect the rising or falling edge of that event to trigger storing the value into the array.
- For example, perhaps a messaging instruction is reading the update value from another device via a serial or network connection (e.g. Modbus). In that case the code could detect the rising edge of the "done" or "complete" status of the messaging instruction to trigger storing the updated value.
- Run a repeating 1s (1000ms) timer in the continuous task, and only store the updated value on the single scan cycle each time that timer expires.
- The timing accuracy of this approach is limited by the duration of the continuous scan cycle, which could be many milliseconds in duration.
- This is a form of the previous "Use an event ..." approach, where the trigger event is supplied synthetically using a timer.
- If you search this forum for the terms "structured text repeating timer" you should find a solution.
- Note that if you are using Siemens TON timers, you may need to buffer the timer output state (.Q) value to a separate variable to get the repeat to work.
- If there is a system instruction to retrieve the current time as [year, month, day, hour, minute, seconds], then each time the seconds value changes you could
- run the loop to store the updated value, and
- copy that current seconds value to a persistent variable to use to detect the next change