Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Delay (Wait) in Structure Text

2016-02-09
2018-03-28
  • manantrivedi77 - 2016-02-09

    Hi,

    How can I achieve delay(wait_ time in Structure Text code?

    Thanks,

     
  • Anonymous - 2016-02-10

    Originally created by: scott_cunningham

    You need to use a TON function block and either an IF statement or a CASE statement, depending on the need complexity.

    For a very basic solution:

    VAR
       Delay : TON;
       Count : INT := 0;
    END_VAR
    Delay(IN:=TRUE, PT:=T#1S);
    IF NOT(Delay.Q) THEN
       RETURN;
    END_IF
    Delay(IN:=FALSE);
    //delay is over.... More code here
    Count := Count +1;
    

    This is a simple example. Be sure to call the TON FB with FALSE once to reset it so it is ready for the next time... This example should result in the variable Count incrementing roughly every second.

    In real life, you probably need something more advanced, but it hopefully helps you out.

    Note: code above was manually typed in - I may have a typo or two

     
  • shooter - 2016-02-14

    in this function block you can use a input var for the time.

     
  • manantrivedi77 - 2016-02-17

    Thanks for the reply guys,
    I tried to duplicate your code, please tell me why the code below is not working. Only the first timer is working not the second one.

    a:=1;
    b:=2;
    c:=3;

    wait5(IN:=TRUE, PT:=T#5S);
    IF NOT (wait5.Q) THEN
    RETURN;
    END_IF

    wait5(IN:=FALSE);

    a:=7;
    b:=8;
    c:=9;

    wait6(IN:=TRUE, PT:=T#5S);
    IF NOT (wait6.Q) THEN
    RETURN;
    END_IF

    wait6(IN:=FALSE);

     
  • Anonymous - 2016-02-18

    Originally created by: scott_cunningham

    In your program, wait6 timer is only reached when wait5 timer expires. Look at the flow of your code - if wait5 timer is not finished, then return - so the rest of your code is skipped. You only need multiple timers if you need to run timers simultaneously.

    Your code must run every PLC scan - it's not like a PC script - you don't go from the first line to the last line and then be "done". Every PLC scan (xxx uS), your code is run. If you need to do steps 1,2,3, then you need a state machine (CASE statement):

    CASE Step OF
       0: //init
          Step := Step + 1;
       1: //first timer routine
          a := 1;
          b := 2;
          c := 3;
          wait(IN:=TRUE, PT:=T#5s);
          IF wait.Q THEN
             wait(IN:=FALSE);
             Step := Step + 1;
          END_IF
       2: //second timer routine
          a := 4;
          b := 5;
          c := 6;
          wait(IN:=TRUE, PT:=T#15s);
          IF wait.Q THEN
             wait(IN:=FALSE);
             Step := Step + 1;
          END_IF
       3: //done - don't do anything more
          ;
       ELSE //code error!!! restart state machine
          Step := 0;
    END_CASE
    

    Now your code executes this way:

    I hope that helps!

     
  • manantrivedi77 - 2016-02-18

    To scott_cunningham,

    It helped a lot, can't thank you enough.

    Thanks again.

     
  • tekisri - 2018-03-25

    Hello !
    I have tried the similar logic and it's not working. I developed a Functional block called "Fan_VFD" and used in a Program "PRG" of CFC type. Everytime when Fan_enable is TRUE;VFD_enable and error_VFD are FALSE, the timer has to count 10sec and execute the following steps. But it isn't happening in this case. The output of the timer is not resetting to False when IN:=FALSE. Please help me resetting the timer.

    [code][/code]
    FUNCTION_BLOCK PUBLIC Fan_VFD
    VAR_INPUT
    diff_pres:REAL;
    VFD_cal_setpoint:REAL;
    VFD_enable:BOOL;
    Fan_enable:BOOL;
    error_VFD:BOOL;
    bypassTimer:TON;
    END_VAR
    VAR_OUTPUT
    VFD_setValue:WORD;
    VFD_status: BOOL;
    Fan_status: BOOL;
    END_VAR

    VAR_IN_OUT
    isolator_status:BOOL;
    byPass_status:BOOL;
    END_VAR


    IF Fan_enable AND NOT VFD_enable AND NOT error_VFD THEN
    isolator_status:=FALSE;
    bypassTimer(IN:=TRUE, PT:=T#10s);
    IF bypassTimer.Q THEN
    bypassTimer.IN:=FALSE;
    VFD_status:=FALSE;VFD_setValue:=0; bypass_status:=TRUE; Fan_status:=TRUE;
    END_IF
    END_IF

     
  • Anonymous - 2018-03-28

    Originally created by: scott_cunningham

    You need to call the timer function block to get it to work and update the output variables. If you look at your code, you only call the function block inside of an IF/THEN block.

    The difference is that your code simply set the input variable to false, but did not execute the function block. You need to do every scan if you want the timer to run:

    bypassTimer();
    
     

Log in to post a comment.