delay(100) is actually 85ms?

Hello,

Today I got to try a Saleae Logic. As a first test while learning how to use it, I wrote a simple app for my Arduino UNO and analyzed it (see code below).

The test worked fine and I could read the 'Hello World!' text coming across on the TX pin of the UNO. For some reason, however, the analyzer is telling me that the delay between the repeating 'Hello World!' string is a very consistent 85.7ms, not 100ms as I expected. Can anyone help explain the discrepancy? Is the delay() function not accurate, or are there other factors here?

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Hello world!");
  delay(100);
}

It's going to take approximately ~13 mS to send that string at that baud rate, so are you sure it is not measuring the difference between when it receives the last byte and the first byte? Try different sized strings to see if it affects it, or put Serial.flush() after the print statement.

You nailed it! I put a Serial.flush() in there and the delay is almost a perfect 100ms. I also tried it without the flush, and the baud rate set to 115200, and it went up to about 99ms.

This tells me that the call to println() must be non-blocking. I suppose data is going into a buffer, and the code execution continues while the serial data gets pushed through in its own time. I'm sure I can find more in the docs for the Serial library.

Thanks for your help!

Thank you for asking the question.
Yet another day i learned something.

Crogdor: You nailed it! I put a Serial.flush() in there and the delay is almost a perfect 100ms. I also tried it without the flush, and the baud rate set to 115200, and it went up to about 99ms.

This tells me that the call to println() must be non-blocking. I suppose data is going into a buffer, and the code execution continues while the serial data gets pushed through in its own time. I'm sure I can find more in the docs for the Serial library.

Thanks for your help!

Yup. Serial.print just puts data into a buffer; the actual communication is interrupt driven.

This tells me that the call to println() must be non-blocking.

True, cars are added to the buffer and then sent async, unless you fill the buffer in which case it blocks until all the has have been added to the buffer.

Mark

I'm sure I can find more in the docs for the Serial library.

In fact to really understand the working you should dive in the code, maybe a bit hard to read at first but definitely the "best documentation". It is in your distribution of Arduino.