1

Is there a timer function or variable in Codesys as in arduino millis() ?

If not, how can I create a timer?

Thanks!

lorex04
  • 15
  • 1
  • 4
  • visit this link: https://stackoverflow.com/questions/49050220/how-can-i-add-a-timer-within-a-function-in-codesys-using-structured-text – N. berouain Dec 19 '19 at 09:31

2 Answers2

2

In CoDeSys function TIME() return time in milliseconds from PLC start. If you want to start the count on the event you can use triggers to create a time point.

VAR
   tStarted, tElapsed : TIME;
END_VAR

fbR_TRIG(CLK := xStart);
IF (fbR_TRIG.Q) THEN
    tStarted := TIME();
END_IF;

tElapsed := TIME() - tStarted;

And rest follows like reset the timer, pause counting, etc.

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
0

You can build one yourself. Here an example:

Declaration part:

FUNCTION_BLOCK FB_Millis
VAR_INPUT
    timer : TON := (IN:=TRUE,PT:=maxTime);
END_VAR
VAR_OUTPUT
    tElapsedTime : TIME;
END_VAR
VAR
    maxTime : TIME := UDINT_TO_TIME(4294967295);
    //timer : TON := (IN:=TRUE,PT:=maxTime);
END_VAR

Implementation part:

timer();
tElapsedTime := timer.ET;

You call it cyclically like this:

fbMillis();

And retrieve the result like this:

tElapasedTime := fbMillis.tElapsedTime;

FB_Millis overflows after 49days 17hours 2minutes 47seconds and 295ms.

If you want to compare the elapsed time from fbMillis.tElapsedTime with another variable you do like this:

IF fbMillis.tElapsedTime < tAnotherTimeVar 
THEN
 ; //Do something
ELSE
 ; //Do something else
END_IF

If you instead just want a simple timer you need the TON Function Block:

Declaration part:

//2 seconds timer
mySimpleTimer : TON := (PT:=T#2s);

Implementation part:

mySimpleTimer();

// your code here

//Start timer
mySimpleTimer.IN := TRUE;

//Check if timer has reached desired time
IF mySimpleTime.Q
THEN
 //Do something here
 mySimpleTimer.IN := FALSE;
END_IF
Filippo Boido
  • 1,136
  • 7
  • 11
  • 1
    First many thanks for your answer! So with fbMillis(); I can start the timer? And what can I do with tElapasedTime := fbMillis.tElapsedTime;? Thanks – lorex04 Dec 19 '19 at 12:14
  • 1
    The arduino reference for millis() says: "Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days." FB_Millis starts automatically, you must just make sure that it is called cyclically. fbMillis.tElapsedTime is the elapsed time since you first called fbMillis() for the first time. – Filippo Boido Dec 19 '19 at 12:22
  • 1
    If you put timer in the VAR_INPUT section of FB_Millis you can also start and reset it yourself. – Filippo Boido Dec 19 '19 at 12:24
  • 1
    ok thanks, it is possible to assign the funcion fbMillis(); to a variable because I need to compare the seconds with another variable. – lorex04 Dec 19 '19 at 13:30
  • 1
    You don't assign fbMillis() to a variable. You can retrieve the elapsed time like I showed you: tElapasedTime := fbMillis.tElapsedTime;. tElapsedTime is a TIME variable wich stores the elapsed time value from fbMillis. You can then compare tElapsedTime with another TIME var. You can also directly compare fbMillis.tElapsedTime with another var without assigning it to tElapsedTime. – Filippo Boido Dec 19 '19 at 13:37
  • 1
    Ohh Right. Thank you so much. – lorex04 Dec 19 '19 at 13:47
  • 1
    Lorex, remember to close your question if it has been answered, thank you: "To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in." stackoverflow.com/help/someone-answers . – Filippo Boido Dec 19 '19 at 13:56
  • 1
    I have one more question about the simple timer. How can I reset it to zero? – lorex04 Dec 23 '19 at 08:18
  • 1
    You control it with the "IN" input, so to reset the timer just set IN to false. If the example code above is called cyclically, the timer will restart every 2 seconds, so depending on your needs you will need a more complex logic.For example you could write a Step-chain where the timer is started in a certain step and it is checked whether the desired time has been reached in the next step.After the time has been reached, the timer is reset and the next step is triggered. – Filippo Boido Dec 23 '19 at 08:53