Implementing a time delay within a function block

jstoupis
2009-02-11
2009-02-13
  • jstoupis - 2009-02-11

    I call a function from a SFC program that I have. I want to implement a time delay within the function block code to delay the execution of code from one line to the next by a certain amount of time (e.g., a few seconds). I am having a hard time finding anything on the Online Help.

    Any help is greatly appreciated!

    Thanks,

    Jim

     
  • ndzied1 - 2009-02-12

    Not quite sure what you are trying to do here.

    Are you trying to delay moving to the next step of the SFC? If so you need to have a timer (TON would be the easiest). When the normal conditions to go to the next step are satisfied, start the time and then use the done bit from the timer to transition to the next step of the SFC.

    If that is not what you are wanting to do you need to explain more...

     
  • jstoupis - 2009-02-12

    Using the TON in the SFC is one way to do, but what I really need is code within a block of an SFC to provide the delay. Let's say I have the following code:

    g := 1;

    g := 2;

    How do I delay assigning the value of 2 to g by 5 seconds, for example? I.e., what code do I need to insert between the two lines above that would emulate a time delay of 5 seconds? I tried using the TON ST code example in the Online Help but that didn't work.

    Thanks,

    Jim

     
  • ndzied1 - 2009-02-12

    Here's a way

    VAR

    MyTON: TON;

    g: INT;

    END_VAR

    ( Call Timer with 5 second preset )

    MyTON( IN:= TRUE, PT:= T#5S);

    IF MyTON.Q THEN

    g:= 2;

    ELSE

    g:=1;

    END_IF;

     
  • jstoupis - 2009-02-13

    I tried this but I get a run-time exception. Here is what my code looks like:

    g :=1;

    MyTON( IN:= TRUE, PT:= T#5S);

    WHILE (MyTON.Q = FALSE) DO

    h := 999;

    END_WHILE

    i := 1;

    It gives me an exception in the while loop, but not when I build it. Basically, I have to select a device (g variable = select) before I operate it (i variable = operate). In both cases, I just have to assign the value of "1" to each variable.

     
  • ndzied1 - 2009-02-13

    If your timer preset is longer than your process watchdog timer then you will get the exception.

    This is because the "flow" of the program is stuck in the While loop until the timer is done. If the "flow" doesn't reach the end of the program before the watchdog is up then <bang> error!</bang>

    You need to use an if instead of a While.

    Zitat:
    In both cases, I just have to assign the value of "1" to each variable.

    What do you want the variables to be before they get assigned a 1.

    In my code example, g will be 1 until the timer preset is reached and then it will become 2 forever after. You can just change the 1 in my program to what you want for the initial value and change the 2 to what you want the value to become after the timer. If you have other variables that change then you can put them within the IF / ELSE as well.

    Perhaps what is confusing you is how my initial value is in the ELSE part of the code and the "after timer" value is in the IF part.

    One if the hardest parts of PLC programming for "computer" programmers to understand is how the program needs to be executed continuously and can't stop anywhere because of the watchdog timer. That timer is there for a good reason so, although you can turn it off, don't be tempted.

     
  • jstoupis - 2009-02-13

    No, your code did not confuse me. I understand what you're doing.

    So, what is the length of the watchdog timer? Where in the project do you find this - in a setting somewhere?

    Thanks,

    Jim

     
  • ndzied1 - 2009-02-13

    I'm not sure how hardware dependent the cycle time is but on the Moeller PLC's we use you start with the Resources Tab, then PLC Configuration, then the Other Parameters Tab. It is called Max Cycle Time.

    This is the default. You usually have the ability to set up other tasks in the PLC and assign their own watchdog timers so it is possible to have one task with a 20msec watchdog and, for example, another with a 500 msec watchdog.

     

Log in to post a comment.