Arduino Cpu Utilization - how?

Hi,

I know there are various memory available routines going around, but I wondered if anyone has come up with a way to measure the main loop time.

We used to have it on our Z80 (Z280) based Cpu's years ago and it measured & displayed the main loop in uS......very handy for seeing what was going on.

Any ideas?

Ian.

Easy enough to do, but with synchronous serial I/O, the reporting affects the results.

if you have a scope or frequency counter, you could turn a pin on and off (set the port bit, not with digitalwrite) and measure the frequency.

Give this a try...

unsigned long LastMicros;

void setup( void )
{
  Serial.begin( 9600 );
  LastMicros = micros();
}

void loop( void )
{
  unsigned long ThisMicros;
  unsigned long Delta;
  
  ThisMicros = micros();
  Delta = ThisMicros - LastMicros;
  LastMicros = ThisMicros;
  Serial.println( Delta );
  
  delay( 1001 );
}

Hi all,

Managed this via a counter in my main loop, then every 40mS (my interrupt loop) measuring how many main loop counts occurred and then translated that into mS.

0.77mS per main loop.

Ian.

0.77mS per main loop

That sounds an awful lot - I assume your serial print time is included in that?
[edit]Fairly empty "loop", like CodingBadly's gives about 8.18us, averaged over 1000 loops[/edit]

Not using serial print at all.......my project has an 4*20 LCD, ethernet shield (web server), servo drive, analogue inputs, digital I/O etc.......550 lines of code.

Using a bunch of other libraries as well, including eeprom.h, pgmspace.h.

This is why I wanted to measure the main loop time.

It's my first Arduino project and I wanted to push it to the limit to see what's capable and what's not. I'm already at the limit of sram.

I guess my next test will be to write a simple sketch with everything ripped out and measure the loop time at it's fastest.

Ian.

Not using serial print at all

Then how do you know how long it took?

The 4x20 LCD, perhaps?

Does that involve serial communication? Mine does.

That sounds an awful lot - I assume your serial print time is
included in that? Fairly empty “loop”, like CodingBadly’s gives
about 8.18us, averaged over 1000 loops

I agree 0.77mS is a lot, so I moved some code from my main loop to my 40mS interrupt loop and immediately it’s down to 0.07mS (70uS).
It was moving the analogRead’s that gained the speed. I didn’t know they were that slow.

Does that involve serial communication? Mine does.

See:-
#include <LiquidCrystal.h>

Ian.