How to add a time delay?

plccopco

Member
Join Date
Sep 2014
Location
Bangalore
Posts
1
Dear All,

I am a beginner in PLC. I am writing a sample structure text application. I have written a while loop and i want to put a timer delay inside the loop.
My code looks like below

where TEMP is INT,
Code:
TEMP := 1;

WHILE	TEMP = 1 DO
    //1. My logic is here

      TEMP := TEMP+1;

	IF TEMP = 1000 THEN
		TEMP := 0;
	END_IF;

[COLOR="Red"][B]//2. Here i want to put a 2 second delay[/B][/COLOR]

	END_WHILE	;

Small help will be highly appreciated.

Thank you all.
 
Search for TON, TOFF OR TP, those are function blocks used to do that.

BUT, be extremely cautious with the loops:

a PLC is as deterministic as it can and it automatically loops (an empty program in a PLC would look like : WHILE TRUE DO (this is the empty space you see at start) ENDWHILE).

And as a PLC is adjusted to process this loop in a certain cycle time you can't stop it more than that limit.

You should do something like:

Code:
TON(IN := TRUE, PT:= t#2s);

A function block requires to be initialized and executed, and usually it needs more than a cycle to perform it's task, therefore you will need something like:


Code:
CASE istate of
  1: 
    mTON(IN := FALSE, PT:= t#2s);
    iState := iState + 1;

  2:
    mTON(IN := TRUE);
    IF (mTON.Q)
    THEN
      ...

Good luck!
 
It is best not to try to stop the PLC scan, but rather have your program work within the PLC's scan loop, the traditional method for which they were originally designed.

Timed events work best with fewer errors when done outside of While-Do or For-Next loops. It appears that your loop is only creating a delay in the first place. For that use a timer, not a loop.
 
It looks like you're trying to stop the PLC scan for 2 seconds, you absolutely cannot do that. PLCs must make it through the entire program quickly or they will fault. You keep parts of your code from executing by using IF statements or CASE statements. Also, the way you have that WHILE loop, it will only run one time. After you do TEMP := TEMP+1; the while check will fail and exit the loop.

You should really stay away from while loops in PLC code in general, using FOR loops instead:

Code:
FOR i:=1 to 1000 DO
  Your Logic
END_FOR

As far as the 2 second delay, use a TON
Code:
MyTON.PT:= T#2s;
MyTON.IN:= SomeConditionToStartTimer;
MyTON()

IF(MyTON.Q) THEN
  Whatever you wanted to wait 2 seconds to do
END_IF
 
This would be so much easier in ladder! LOL

This guy is probably a computer programmer that has just encountered his first RTOS and has no idea how different it is from event based programming. Using pictograms isn't going to make any difference. Besides, looping in ladder is a pain in the ***.
 
FOR and WHILE are perfectly usable and they are helpful sometimes.

The rule of thumb should be: use whichever suits you best, but be sure that none of them will surpass the cycle time.

See:
Code:
FOR i:=1 to 100 DO
  Logic_that_takes_1ms_to_execute;
END_FOR
If your cycle time would be 10ms you would be in trouble.

In that case you should divide the code in 10 parts/loops that could be the same of course, but only processing one part each time:

Code:
WHILE (i < 100) DO
  Logic_that_takes_1ms_to_execute;
  i := i + 1;
  if (i >= 9) THEN break; END_IF  (* Here you are breaking the loop. *)
END_WHILE
(* Next time you'll be here from i = 9 so you will continue counting without killing you cycle time and taking the most of your device.  If you want to do that it is advisable that you would leave some extra safety time for future code additions. i.e. if cycle time equals 10ms  you should do 9 or less iterations of 1 ms only.*)

Some times is advisable to make more than one thing inside a same scan cycle to take advantage of the calculus capabilities of your device...
 
The best way to do this is to have a state machine and use a case statement. Then you must use code like this.

SystemTicks0 = GetSystemTicks() // in case n

Then in case n+1

If GetSystemTicks() > SystemTicks0 + DelayTicks then
n = n+1

Don't use while or for statements.

In motion control letting the customer use WHILE, FOR or REPEAT statements is a NO-NO.

A local company that uses Delta Tau motion controllers doesn't allow their programmer to use loops. Just the Delta Tau equivalent of a CASE or SWITCH statement.
 
TEMP := 1; WHILE TEMP = 1 DO (* so temp = 1 *) //1. My logic is here TEMP := TEMP+1; (* here temp is not one anymore so your while loop stops here*) IF TEMP = 1000 THEN TEMP := 0; END_IF;
so now wait (oops a PLC can not stop) 2 seconds. //2. Here i want to put a 2 second delay END_WHILE ;
make a POU called adder, put here your code so if temp >= 1000 then temp = 0
call this program every 2000 milliseconds from the taskmanager.
 

Similar Topics

Hi, I have questions. I have Analog Input that need to put into Ignition Designer. But I don't know how to put?
Replies
1
Views
116
I have just installed Studio 5000 V30.11 on my Windows 11 Pro machine. First I had an "Invalid Pointer" error that required me to update Factory...
Replies
2
Views
116
Im trying to create a level indicator for water Tank i have used the ADD function while the pump is on and level increasing everything works...
Replies
33
Views
1,017
We are trying to poll data coming from a PLC for remote monitoring we have the IP address of the PLC and the default port number and the path is...
Replies
25
Views
568
Back
Top Bottom