Arduino is Running Very Slowly

I am trying to use fine timing with delayMicroseconds, but I have noticed that my arduino takes so long to process each command that it really doesn’t matter what value I enter. I try:

void setup() {
  Serial.begin(9600);
}
void loop() {
  for (int i = 0; i < 100; i++) {
    Serial.println(micros());
  }
}

But the values printed have a difference of over 10,000.
Here are some of the values I get in order:

2822724
2832084
2841444
2850804
2860164
2869524
2878884
2888244
2897604
2906964
2916324
2925684

Is the arduino supposed to take this long to execute basic commands, or is something wrong?

I am using an arduino uno, I believe rev 3.

EDIT: The delay is definately coming from the Serial.println() statement, but other commands seem to take about as long, making it delayMicroseconds completely useless.

it's the Serial.println() that's slowing things down so much. You can't consider Serial.println() to be a "basic command". You can speed it up a bit by increasing the baud rate from 9600 but you really need to avoid things like in code that needs to run fast. You need to understand the Uno is only running at 16MHz.

Ystem: other commands seem to take about as long, making it delayMicroseconds completely useless.

Which other commands?

Try this and you will be amazed at what you see when the effect of Serial.print() is eliminated from the experiment:

unsigned long microDump[100] ;
void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 100; i++) {
    microDump[i] = micros();
  }
  for (int i = 0; i < 100; i++) {
    Serial.println(microDump[i]);
  }
}
void loop() { }

Let's examine your results:

- µs -  difference
2822724 
2832084 9360
2841444 9360
2850804 9360
2860164 9360
2869524 9360
2878884 9360
2888244 9360
2897604 9360
2906964 9360
2916324 9360
2925684 9360

OK, pretty consistently 9360 µs between each measurement.

Now let's do some maths. I like maths, because it helps explain what happens in the world.

  Serial.begin(9600);

At 9600 baud, and at 10 bits per byte (8 data, one start and one stop) each byte will take 1/960 seconds to send. That is:

1/960 = 0.0010416666 seconds

You have shown 7 bytes of data, plus carriage return and linefeed, which is 9 bytes. Thus we expect each line to take 9 * 1/960 seconds, that is:

 9 * 1/960 = 0.009375 seconds

You are getting 9360 µs which means it is running slightly faster than expected (15 ns). This could be explained because micros() has a granularity of 4 µs.


I am trying to use fine timing with delayMicroseconds, but I have noticed that my arduino takes so long to process each command that it really doesn't matter what value I enter.

Your posted code doesn't have delayMicroseconds in it, so I can't quite see how you can make this claim.

Ystem: EDIT: The delay is definately coming from the Serial.println() statement, but other commands seem to take about as long, making it delayMicroseconds completely useless.

What other commands? Post your code. It is not completely useless.

And here are some of the values that I got (using a SparkFun Redboard and hence the little difference with the numbers at the end).

  # usecs   delta
  1 48
  2 180       132
  3 356       176
  4 536       180
  5 716       180
  6 892       176
  7 1076      184
...
...
 91 632484   8320
 92 640804   8320
 93 649124   8320
 94 657444   8320
 95 665764   8320
 96 674084   8320
 97 682404   8320
 98 690724   8320
 99 699044   8320
100 707364   8320

At iteration #28 the delta became a steady 8320.

Your picture did not show the complete story ;)

NickGammon has touched on most of it. What was omitted is that the serial port has a software buffer of 63 bytes. Serial.println dumps characters in that buffer as fast as the rest of your code allows. Once that buffer is full, Serial.println will wait till sufficient bytes have been send and it has been able to dump all characters in the buffer before it returns to the calling function.

And hence the massive difference between the first few iterations and the last ones.

[quote author=Nick Gammon date=1477818722 link=msg=2980555]Now let's do some maths. I like maths, because it helps explain what happens in the world. [/quote] You don't say. Funny how that works out, it's almost like numbers actually mean something.

This changes everything!

sterretje: NickGammon has touched on most of it. What was omitted is that the serial port has a software buffer of 63 bytes.

I omitted that bit because the quoted figures didn't show the faster start-up. You are right, of course. If you are doing other things you can get away with sending serial data in bursts.