Arduino hangs for 40ms - is it related to watchdogs ?

Hi everyone,

When running the most simple code, Arduino hands for a few ms. Why? How to avoid that and have the Arduino always running ?

Here is the example :

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

void loop() {
   Serial.println("log");
}
/* result :
...
14:53:34.696 -> log
14:53:34.696 -> log
14:53:34.696 -> log 
14:53:34.735 -> log <- Here there is a 39ms delay. Why
14:53:34.735 -> log
14:53:34.735 -> log
14:53:34.735 -> log
...

*/

This issue might be related to watchdogs but wdt_disable(); is not working.

I’m asking that because I am experiencing an issue with I2C communication. This 30ms pause drastically reduce the communication rate of i2C.

Serial.begin(9600);The 1960s called - they want their bitrate back

No there is nothing to do with bitrate.

I recommend to try this code :

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

unsigned long lastFiredTime;

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long now = millis ();
  unsigned long interval = now - lastFiredTime;
  lastFiredTime = now;

  Serial.print(now);
  Serial.print("\t");
  Serial.println(interval);
  delay(1);
}

Tell me if you also notice a difference between the timestamp interval and the actual interval

Yes, that will print fine until the serial buffer gets full. Then it will block until there is room to add your message to the buffer. Increase the bitrate and see what happens.

Watchdog doesn’t slow code down. It resets the board when something else hangs it up. You don’t even have it turned on in this code.

AWOL:
Serial.begin(9600);The 1960s called - they want their bitrate back

Maybe a bit early? I wonder if you could get that rate using an acoustic coupler. My first modem in the 80’s was 300 baud (it was all I could afford) but was fine for being a .point on a BBS.

Riva:
Maybe a bit early? I wonder if you could get that rate using an acoustic coupler. My first modem in the 80’s was 300 baud (it was all I could afford) but was fine for being a .point on a BBS.

Your acoustic coupler in the 1960s was probably connected to a mechanical TTY, so any bit rate beyond 100 was overkill.
But computers spoke to other computers much quicker than that.

Marcteys:
No there is nothing to do with bitrate.

OK, right.

Delta_G:
Yes, that will print fine until the serial buffer gets full. Then it will block until there is room to add your message to the buffer. Increase the bitrate and see what happens.

It's not changing anything. I changed the baudrate of to 115200 in both the arduino code and the computer settings: I still have the constant 40ms hang of the arduino

This is something to do with the receiving computer then, its also buffering serial input...

Trust us, the Arduino runs constantly without such 40ms pauses.

So is it possible to send every 10ms rather than 40ms ? (reduce this buffer length/timeout)

40ms sounds suspiciously reasonable for the delay that would happen on a multi-tasking OS, when other tasks have "things to do."

Here's some output from your second sketch:

15:41:53.029 -> 12188    10

So there's about 10ms between each string. This is expected, because you're outputting about 11 characters. At 9600bps, that will take about 11ms, once you've reached "steady state" with the buffers full and each new character waiting for one to finish transmitting.

15:41:53.067 -> 12198    10
15:41:53.067 -> 12209    11
15:41:53.[b][color=red]067[/color][/b] -> 12219    10
15:41:53.[color=red][b]103[/b][/color] -> 12229    10

And there was the first 40ms gap in the timestamps, even through the arduino thinks only 10ms is between the two messages.

15:41:53.[b][color=green]103[/color][/b] -> 12239    10
15:41:53.[b][color=green]103[/color][/b] -> 12251    12
15:41:53.[color=green][b]103[/b][/color] -> 12261    10

But now, the host-side process "catches up" with the data it has been receiving, dumping the 40ms worth of messages from the Arduino in "zero" time (all at the "...103" timestamp)

15:41:53.139 -> 12271    10
15:41:53.139 -> 12281    10
15:41:53.139 -> 12292    11

So, yeah. Definitely host-side delays...

Then it will block until there is room to add your message to the buffer.

Note that the Arduino does not "block until there is room for the message." It blocks until there is room for ONE BYTE of the message, which only takes one character time. (may be different for direct USB connections.)