Read current milliseconds from Portenta Machine Control Internal Clock

Trying to calc elapsed time for a function in Structured Text on this hardware.

Can I do this with out of the box setup or do I need to install additional libraries for this?

Also, where can I find a comprehensive list of commands for this Hardware including Structured Text commands?

Thanks

Anyone able to help to create a timing loop to turn on some outputs?

Hi @phoneman,

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.

PROGRAM main

VAR
TestTimer : TON;
TestButton : BOOL;
ElapsedTime : UDINT;
END_VAR

TestTimer(IN := TestButton, PT := 999999);

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)