title: How can I add a timer within a function in codesys using structured text?
body: <p>I'm having a problem with structured text in Codesys V3.5 SP9 Patch 5. What I want to do is to be able to use a timer within a function created by me, which is called in a POU. I've done the same without using function by putting timer directly into POU and it is working.</p>
  
  <p>My function declaration:</p>
  
  <pre><code>FUNCTION AssignDOORStatus : USINT
  VAR_INPUT
  
      DDUC_ComSta_Dcux_x: BOOL; //No communication
      DDUC_DCUxEmHdler_x: BOOL; //Emergency handler
  
  END_VAR
  VAR
  
      Timer: TP; //Timer to do intermittence between current doors status and emergency handler
      CurrentDoorStatus: USINT;
      TONProcessTime: TIME := T#1S; //TONProcesTime
  
  END_VAR
  </code></pre>
  
  <p>My function code:</p>
  
  <pre><code>IF DDUC_ComSta_Dcux_x THEN
  
      CurrentDoorStatus := 0;
  
  ELSE
  
      CurrentDoorStatus := 1; 
  
  END_IF
  
  
  IF DDUC_DCUxEmHdler_x THEN
  
      Timer(IN := NOT Timer.Q, PT := TONProcessTime); //Timer starts
      Timer();
  
      IF Timer.Q THEN //When TONProcessTime has gone by
  
          IF AssignDOORStatus &lt;&gt; CurrentDoorStatus THEN
  
              AssignDOORStatus := CurrentDoorStatus;
  
          ELSE AssignDOORStatus := 10;
  
          END_IF
  
      END_IF
  
  ELSE
  
      AssignDOORStatus := CurrentDoorStatus;
  
  END_IF
  </code></pre>
  
  <p>My code in POU main:</p>
  
  <pre><code>testdoor := AssignDOORStatus(DDUC_ComSta_Dcu1_S1_T,DDUC_DCU1EmHdler_S1_T);
  </code></pre>
  
  <p>This code is used to assign to "AssignDOORStatus" 0 or 1 depending on variable "DDUC_ComSta_Dcux_x " and then, when "DDUC_DCUxEmHdler_x " is true, it flips "AssignDOORStatus" value from "0 or 1" to 10, using timer.</p>
  
  <p>I have to call in POU many times this function.</p>
  
  <p>Thanks in advance!</p>