CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Mon Dec 17, 2018 5:31 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Feb 09, 2016 10:44 pm 
Offline

Joined: Thu Dec 18, 2014 4:34 pm
Posts: 6
Hi,

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

Thanks,


Top
   
PostPosted: Wed Feb 10, 2016 1:02 am 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 291
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:
Code:
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

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
PostPosted: Sun Feb 14, 2016 10:34 pm 
Offline
Frequent User
Frequent User

Joined: Thu Jun 14, 2007 9:49 am
Posts: 985
in this function block you can use a input var for the time.

_________________
shooter@home.nl
computer shooter
paul deelen
jacob van wassenaerstraat 29
NL 5224 GG 's-Hertogenbosch
skype shooter paul.deelen
phone +31736139136


Top
   
PostPosted: Wed Feb 17, 2016 10:07 pm 
Offline

Joined: Thu Dec 18, 2014 4:34 pm
Posts: 6
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);


Top
   
PostPosted: Thu Feb 18, 2016 1:55 am 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 291
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):
Code:
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:
  • PLC scan 1 - case 0 executes (step incremented)
  • PLC scan 2 - case 1 executes (timer starts - 5 seconds)
  • PLC scan 3 - case 1 executes (timer running)
  • PLC scan 4 - case 1 executes (timer running)
  • ...
  • PLC scan xx - case 1 executes (timer expires, timer disabled, step incremented)
  • PLC scan xx+1 - case 2 executes (timer starts - 15 seconds)
  • ...

I hope that helps!

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
PostPosted: Thu Feb 18, 2016 2:57 pm 
Offline

Joined: Thu Dec 18, 2014 4:34 pm
Posts: 6
To scott_cunningham,

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

Thanks again.

:D


Top
   
PostPosted: Sun Mar 25, 2018 8:36 pm 
Offline

Joined: Tue Feb 27, 2018 11:50 am
Posts: 6
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


Top
   
PostPosted: Wed Mar 28, 2018 3:24 am 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 291
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:

Code:
bypassTimer();

_________________
Scott Cunningham
KEB America, Inc.
www.kebblog.com
www.kebamerica.com


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 8 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited