Codesys Wait function block

Hi. I'm writing an CODESYS program with ST.
In diffrent parts of my main program i need my program to pause for a while and continue running after a time elapsed. So i defined Wait function block as below and reused it where it's need (multiple times):

delectation part:

FUNCTION_BLOCK Wait
VAR_INPUT
time_in:TIME;
END_VAR

VAR
ton1:TON;
END_VAR

----------------------------------
body part:

ton1(in:=TRUE,PT:=time_in);
WHILE ton1.Q=FALSE DO
ton1(PT:=time_in);
END_WHILE
ton1(in:=FALSE);

The function block works as expected, but is consume cpu very much, and watchdog stops my program.

what is the alternative way of doing this?

all i want is some thing like this:

Line 1
Line 2
Wait 5 seconds
Line 4
Wait 10 seconds
Line 6
...

Thanks.
 
I would use the done bit (Q) of a timer to let the next step/line continue. For example, start a timer when line 2 is complete. When that timer is complete, allow the next line to process. I would also probably use a sequence step to help keep things clean. I also find it useful to make sure that my timer are being called outside of IF statements to make sure that they are processed each PLC scan.

Code:
sequence : DINT;
t_line2Complete : TON;
line2Complete : BOOL;
firstScan : BOOL := TRUE;

IF (firstScan) THEN
    sequence := 1;  /* line 1 */
    line2Complete := FALSE;
    firstScan := FALSE;
END_IF

IF (sequence == 1) THEN
    [do line 1 things]
    sequence := 2;
END_IF

IF (sequence == 2) THEN
    [do line 2 things]
    line2Complete := TRUE;
    sequence := 3;
END_IF

t_line2Complete(PT := t#5s, IN := line2Complete);

IF ((sequence := 3) AND (t_line2Complete.Q)) THEN
    [do line 3 things]
    line2Complete := FALSE;
    sequence := 4;
END_IF
...

Hopefully that gives you an idea of something that you could do.
 
I would use the done bit (Q) of a timer to let the next step/line continue. For example, start a timer when line 2 is complete. When that timer is complete, allow the next line to process. I would also probably use a sequence step to help keep things clean. I also find it useful to make sure that my timer are being called outside of IF statements to make sure that they are processed each PLC scan.

Code:
sequence : DINT;
t_line2Complete : TON;
line2Complete : BOOL;
firstScan : BOOL := TRUE;

IF (firstScan) THEN
    sequence := 1;  /* line 1 */
    line2Complete := FALSE;
    firstScan := FALSE;
END_IF

IF (sequence == 1) THEN
    [do line 1 things]
    sequence := 2;
END_IF

IF (sequence == 2) THEN
    [do line 2 things]
    line2Complete := TRUE;
    sequence := 3;
END_IF

t_line2Complete(PT := t#5s, IN := line2Complete);

IF ((sequence := 3) AND (t_line2Complete.Q)) THEN
    [do line 3 things]
    line2Complete := FALSE;
    sequence := 4;
END_IF
...

Hopefully that gives you an idea of something that you could do.

Thanks for your reply. I've find better way to that by sleeping running task.
 
you best use SFC for this, but also possible in ST

if line 1 and line 2 done and NOT timer1.IN then Timer1.IN=TRUE....
if Timer1.Q start line4
etc.
The program must be scanning all the time.
or use state programming.
 

Similar Topics

Hi everyone, I'm New here I am currently working on a personal/learning project involving industrial communication and experimenting with a...
4
Replies
227
Views
Hi everyone! I'm not going to lie and claim this is my work at all or that I was the one to figure this out. Definitely all ChatGPT lol. But I...
1
Replies
214
Views
I asked this question in a CODESYS forum, but didn't get any replies , as I don't know how busy that forum is. Was wondering if anyone here could...
4
Replies
434
Views
Hi, I'm completely new to CODESYS so I'm guessing this is a basic error in my HMI Visualisation. When I try to go online with my Simulation, I get...
2
Replies
298
Views
I need Codesys v2.3 and eCockpit "the Programming software of WAGO". do you have installation files?
5
Replies
634
Views
Top