Loop cycle time

Have you read the discussion on Mr. Gammon's 'ProfileTimer' ?

.

larryd:
Have you read the discussion on Mr. Gammon's 'ProfileTimer' ?

Gammon Forum : Electronics : Microprocessors : Timer library (ProfileTimer) - times code on the Arduino

.

larryd:
Have you read the discussion on Mr. Gammon's 'ProfileTimer' ?

Gammon Forum : Electronics : Microprocessors : Timer library (ProfileTimer) - times code on the Arduino

.

Thank you. I have read and downloaded the library. Will test it out.

Robin2:
Then write a short test program that allows you to figure out how to calculate the loop time. When you know how then you can apply the technique to your big program.

...R

Thats what I did. Then got confused as to how accurate it would be.
Tested on my original uno just toggeling pin on and off with serial print. Took just over 4ms. Then tested same way using my mega from china. Took just over 2ms.
Thats when I decided to post here.

I assume the code to calculate my loop elapsed time is correct if I remove the serial print. No one has mentioned anything other than the serial causing overhead.

Welsyntoffie:
I assume the code to calculate my loop elapsed time is correct if I remove the serial print. No one has mentioned anything other than the serial causing overhead.

That was certainly not the impression I got and now that I look at the code in your Original Post I do not think it is suitable.

If you want to measure the time that the code in loop() takes then the method suggested in Reply #7 is the best.

What I would do is put the code I want to time into a function and call that function 100 or 1000 times from setup() recording the time before and after the iterations. Something like

void setup() {
  // usual prelminary stuff
  startMicros = micros();
  for (int n = 0; n < 1000; n++) {
    myTestFunction();
   }
   endMicros = micros();
   duration = endMIcros - startMicros;
   Serial.println(duration);
}
void loop() {
}

...R

Robin2:
That was certainly not the impression I got and now that I look at the code in your Original Post I do not think it is suitable.

If you want to measure the time that the code in loop() takes then the method suggested in Reply #7 is the best.

What I would do is put the code I want to time into a function and call that function 100 or 1000 times from setup() recording the time before and after the iterations. Something like

void setup() {

// usual prelminary stuff
  startMicros = micros();
  for (int n = 0; n < 1000; n++) {
    myTestFunction();
  }
  endMicros = micros();
  duration = endMIcros - startMicros;
  Serial.println(duration);
}
void loop() {
}




...R

I'm going to sneak a question in on this thread. I'll keep it breaf. @Robin2 .. With your code, could one simply use loop() instead of a myTestFunction()? Or is that not allowed?

EDIT: Breaf? Was more tired than I thought. How about "brief"?

Thanks for answering Robin2! I was pretty sure one couldn't get away with that. But I guess there's nothing saying you can't put everything inside loop() into another named function and still use your method.

Robin2:
What I would do is put the code I want to time into a function and call that function 100 or 1000 times from setup() recording the time before and after the iterations.

Thank you. I am using time and alarm library.
Some functions gets called every 5seconds and some every 10 seconds. These intervals gets set in the setup.
I assume if I put your suggestion at the end of my setup then my main code would run as normal.

Will test this as soon as I get home.

DangerToMyself:
I'm going to sneak a question in on this thread. I'll keep it breaf. @Robin2 .. With your code, could one simply use loop() instead of a myTestFunction()? Or is that not allowed?

No. That is not an option because of the way the Arduino system uses the loop() function.

Keep things simple.

...R

Welsyntoffie:
Some functions gets called every 5seconds and some every 10 seconds. These intervals gets set in the setup.
I assume if I put your suggestion at the end of my setup then my main code would run as normal.

Start simple. Just figure out how to time a function that has no complicated timing.

Trying to write a program to measure time taking account of things that happen at huge intervals like 5 and 10 seconds does not make sense. Just figure out how long each of those actions takes and calculate the combined (or average) time with your calculator - or pencil and paper.

...R