How to Store Real-Time Int Values in an Array efficiently

Question:

Greetings, I am facing a challenge with storing the values of an int variable that updates every second in an array. I need to retain the 100 most recent values in the array while the PLC is in operation. Utilizing a FOR loop hasn't been successful as it executes on each PLC cycle without waiting for a full second to populate the array. I am seeking a viable solution to address this issue. Thank you for your assistance.

Top Replies

Akgaurihar asked how to efficiently store updated int values every second in an array within a PLC environment. The goal is to continuously fill the array with values until the PLC stops running. Using a FOR loop proved ineffective as it executed per PLC cycle and did not wait for a second to fill the array. Understanding the importance of time in PLC programming is crucial, as the PLC scan cycle acts as the clock. The key is to find the right loop that allows storing values only once per second. Different PLC models and coding environments may require various approaches to achieve this. One solution is to implement a timed interrupt task that runs every second, ensuring accuracy in timing. Alternatively, detecting a specific event associated with the updated value and triggering the storage process can also work. Utilizing timers within the continuous task is another method, but timing accuracy may be affected by the scan cycle duration. Researching "structured text repeating timer" on forums can provide additional insights into implementing these solutions. Overall, the focus is on finding a method that aligns with the PLC system and programming expertise while ensuring precise storage of values in the array.

Akgaurihar inquired about storing 100 values in an array while the PLC is operational. The array will continuously store values as long as the PLC is running. It may prove challenging to save values in a PLC that is not in RUN mode, unless external software is utilized to input values in program mode.

In the world of PLC programming, understanding the significance of time is crucial. The PLC scan cycle acts as the clock, with potentially up to a thousand scan cycles happening within a single second. It is essential to ensure that your code only stores values during specific scan cycles to optimize efficiency. To achieve this, you must identify an event that changes precisely once per second. The method you choose will depend on various factors, such as the required timing accuracy and the specific PLC model being used (Siemens, Allen-Bradley, Codesys, etc.). One possible solution is to implement a timed interrupt task that runs every second, allowing you to store updated values in an array. This approach offers high timing accuracy and requires configuring the PLC accordingly. Another option is to detect a rising or falling edge in the stored integer value update event to trigger the storage process. If you are using TwinCAT 3 and Structured Text for programming, employing a TON timer with a 1-second preset and a constantly TRUE input can help with your calculations. By capturing the rising edge of the timer output, you can perform your calculations every second and store the results in an array with shifting positions. For further assistance with implementing a repeating timer in Structured Text, consider searching for solutions using terms like "structured text repeating timer." Remember to adapt the instructions based on the specifics of your PLC model and programming environment to ensure optimal functionality.

Ken Moore mentioned that storing values in a PLC without it being in RUN mode can be challenging. However, it is possible to have external software write the values in program mode. This could be useful for prefilling an array on startup. In Siemens PLC programming, you can use a Startup OB (such as OB100) to execute code when the PLC boots up but before it is fully in RUN mode. By having default values in the array, you can avoid filling it up every time the PLC starts up.

Indeed, the suggestion is to wait 100 seconds during start-up as each value takes 1 second to process. However, a more efficient approach could involve incorporating a loop within OB100 with a time delay. Alternatively, to streamline the process, one could execute the array population routine prior to all other functions on any platform. In my experience, I have typically populated the array with the current values at the beginning to quickly get operations up and running smoothly.

Hi there! It sounds like you could use a timer or delay action of some sort. Timing synchronization to your PLC's clock could prevent the loop from executing before a full second has passed, therefore not populating before the variable updates. Adding a time-stamp to each of your variables may also be a practical solution to track and store them accordingly. You could then utilize an array that acts as a stack, pushing new values in and popping the oldest out. This will ensure you always have the 100 most recent values in your array.

Have you tried using a timer within your PLC code? By using a timer, you could ensure the code you want to execute only runs every 1 second regardless of the PLC cycle time. Then within this timed execution, use a circular buffer for storing your values. This would always keep the last 100 values, rolling off the oldest one as new ones arrive. Just a thought, this method has worked pretty well for me in similar scenarios.

More Replies →

Streamline Your Asset Management
See How Oxmaint Works!!

✅   Work Order Management

✅   Asset Tracking

✅   Preventive Maintenance

✅   Inspection Report

We have received your information. We will share Schedule Demo details on your Mail Id.

To add a comment, please sign in or register if you haven't already..   

Frequently Asked Questions (FAQ)

FAQ: FAQs:

Answer: 1. How can I efficiently store real-time int values in an array? - One efficient way to store real-time int values in an array is by utilizing a circular buffer. This data structure allows you to overwrite the oldest values with the newest ones, ensuring you maintain the most recent 100 values in the array.

FAQ: 2. How can I ensure that the array is updated every second without interference from the PLC cycle?

Answer: - To overcome the issue of the FOR loop executing on each PLC cycle, you can consider implementing a time-based function or timer interrupt to trigger the array population at regular intervals, such as every second. This way, you can update the array independently of the PLC cycle.

FAQ: 3. What are some other alternatives to using a FOR loop for storing real-time values in an array?

Answer: - Instead of relying on a FOR loop, you can explore using asynchronous programming techniques or event-driven programming to handle the real-time updates of the int values in the array. By decoupling the array population process from the PLC cycle, you can achieve more efficient and accurate data storage.

Ready to Simplify Maintenance?

Join hundreds of satisfied customers who have transformed their maintenance processes.
Sign up today and start optimizing your workflow.

Request Demo  â†’