How can I achieve delay(wait_ time in Structure Text code?
Thanks,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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_VARDelay(IN:=TRUE, PT:=T#1S);IFNOT(Delay.Q)THEN
  RETURN;END_IFDelay(IN:=FALSE);//delayisover.... MorecodehereCount :=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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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):
CASEStepOF
  0: //init
   Step :=Step+1;
  1: //firsttimerroutine
   a :=1;
   b :=2;
   c :=3;
   wait(IN:=TRUE, PT:=T#5s);
   IFwait.QTHEN
     wait(IN:=FALSE);
     Step :=Step+1;
   END_IF
  2: //secondtimerroutine
   a :=4;
   b :=5;
   c :=6;
   wait(IN:=TRUE, PT:=T#15s);
   IFwait.QTHEN
     wait(IN:=FALSE);
     Step :=Step+1;
   END_IF
  3: //done-don't do anything more
   ;
  ELSE//codeerror!!!restartstatemachine
   Step :=0;END_CASE
Now your code executes this way:
I hope that helps!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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();
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
How can I achieve delay(wait_ time in Structure Text code?
Thanks,
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:
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
in this function block you can use a input var for the time.
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);
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):
Now your code executes this way:
I hope that helps!
To scott_cunningham,
It helped a lot, can't thank you enough.
Thanks again.
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
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: