How to Create a Ladder Timer in Code for Specific Duration of 2 Seconds

Question:

Hey everyone, I'm facing a challenge with a seemingly simple question that I've managed to overcomplicate. I have a variable named "light" that I need to remain illuminated for a specific duration of 2 seconds after both Condition1 and Condition2 are met. These conditions will only be true for a short period, so I want the light to stay on for the full 2 seconds following their initial activation, regardless of any subsequent changes in their status. If either Condition1 or Condition2 become true again while the light is still on, I need it to remain illuminated. How can I achieve this functionality in my code? This is my current approach.

Top Replies

Can you identify the platform being used in this scenario? Some observations and questions have arisen. Firstly, is the -|P|- instruction signaling a pulse? Additionally, is the -(S)- instruction a "SET" command that activates the HoldLight? It seems that TON is functioning as an on-delay timer, with its Q turning on once the timing is complete. In the event that the timer is not running, either Moderate or HighIntensity will trigger the HoldLight and initiate the timer, which will subsequently expire. The LightHoldTimer.Q instruction will remain inactive due to the deactivation of the |P| instructions and the absence of a reset command for the HoldLight. It might be beneficial to incorporate an off-delay timer triggered by the |P| pulse. While the TOF.Q is active, the light remains illuminated. Please note that the analysis is primarily applicable to AB PLCs, specifically models 5, 500, and 5000. If your platform differs significantly, outcomes may vary.

If either Condition 1 or 2 becomes true again while the light remains illuminated for 2 seconds, the desired outcome is for the light to stay on continuously. The key concern here is the repetition of trigger conditions within the two-second interval of the light being on. Should Condition 1 or 2 be met while the light is still on from a previous event, do you wish for the two-second countdown to restart? In simpler terms, should the light remain on during any scan cycle where either Condition is met, as well as for an additional 2 seconds after the last scan cycle where a Condition transition occurred? If the answer is yes, then the Timer OFF-delay (TOF) instruction, as suggested by @joseph_e2, is ideally suited for this scenario. The TOF logic aligns perfectly with the described requirements and can be implemented using just three instructions on a single rung.

Give this a go - the contacts with an up arrow function the same way as your "P" contacts. The initial rung acts as a reset for the timer, ensuring that if either trigger happens again, it won't activate unless the latch is off first. The "P" contacts reset in just one scan before moving on to the next scan, ensuring that it only happens once the latch has been set. When the triggers first pulse, the latch is enabled for the timer to start counting. The latch is then reset once the timer reaches its end. The timer will run on the latch for two seconds, but if another trigger pulse occurs, it resets and keeps the latch (light) on for an additional two seconds.

In search of a solution, users discussed using an off-delay timer triggered by a pulse and the Timer Off-Delay (TOF) instruction. The TOF logic was lauded for its simplicity, requiring only three instructions on a single rung. One user recommended a reset for the timer using triggers similar to the "P" contacts, allowing for the timer to reset if the latch is off before the next scan. This latch procedure, along with the TOF method, proved to be effective solutions. Thank you to all for the assistance!

It sounds like you need a timer function that triggers the light to stay on for 2 seconds once both conditions are met and ignores any subsequent changes during this period. In Java, you might want to use something like a ScheduledExecutorService to schedule a task that turns off the light after 2 seconds. In JavaScript, you could use the setTimeout function. Be sure to control the task execution so that even if the conditions are met again during the 2 second period, the light remains on and the timer doesn't reset. This is just a high-level orientation—please share the specific programming language you are using so we can get into details.

It appears like your challenge could be addressed using a timer mechanism. When both conditions are met, start a timer for the required duration (2 seconds in your case) and illuminate the light. When the timer is running, even if the conditions change, the light stays on. If during this period the conditions become true again, simply extend or restart the timer to ensure the light remains on for the full two seconds from that point. Most programming languages have libraries or functions to help with timers, e.g., setTimeout in JavaScript or Timer class in Java. You might want to look into those.

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: How can I make a variable remain illuminated for a specific duration of 2 seconds after meeting certain conditions in code?

Answer: You can achieve this functionality by implementing a timer mechanism that keeps the variable illuminated for the desired duration after the conditions are met.

FAQ: What should I do if I want the variable to stay illuminated for the full 2 seconds even if the conditions change during that time?

Answer: To ensure the variable remains illuminated for the full 2 seconds after meeting the conditions, you can create a logic that ignores subsequent changes in the conditions while the timer is active.

FAQ: How can I prevent the variable from turning off if the conditions become true again while the light is still on?

Answer: To keep the variable illuminated when the conditions become true again while the light is still on, you need to design your code to maintain the state of the light based on the initial activation and not be affected by subsequent changes in conditions during the 2-second duration.

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  â†’