You do not need any extra libraries for this kind of functionality. It can be as simple as the example code below:
void setup() {
unsigned long currentMillis = 0;
}
void loop() {
// Get current time
currentMillis = millis();
// Run your code
testfunction();
// Calculate time difference taken for function to run
unsigned long loopTest = millis() - currentMillis;
// Output the result
Serial.println(loopTest);
}
Have you tried the TON Function Block provided by Structured Text?
This program makes a TON timer called TestTimer that is started and stopped by a boolean variable called TestButton. When TestButton is TRUE or 1, the timer advances. When TestButton is FALSE or 0, the timer stops.
The Elapsed Time shows how long (in milliseconds) the timer has been been advancing.
IF TestButton THEN
ElapsedTime := TestTimer.ET;
END_IF;
You can download this program to your PMC and use the Watch window in Arduino PLC to see the timer in operation. You can change the value of TestButton from the Watch window and watch the timer and Elapsed Time.
If you change the timer statement to
TestTimer(IN := NOT TestTimer.Q, PT := 4000);
the timer will restart every 4 seconds. You can use TestTimer.Q as a value for a timing loop to turn things on or off every 4 seconds.
(sorry I never posted before and don't know how to format code properly. I will work on that and learn before my next posting)