Lagging/slow serial monitor at -xx.xx but not at xx.xx

Sorry for the confusing title.

I recently hooked up an MPU6050 gyroscope, configured as an angle sensor. The sample rate is set to 1000 Hz. I am printing the current angle (double), which is within [-45.00;45.00] degrees.

When i Serial.print(angle), every value is printed just fine from [-9.99;45.00] degrees, but if i try to print any negative angle below -9.99, the monitor starts lagging.

My guess is due to the signed value, I.E a total of 5 digits, -xx.xx. This works fine with a sampling rate of 500 Hz and every angle value above -9.99 works fine at 1000 Hz as well. I’m using an Arduino Nano and the Baud rate is set to 115200. 230400 and 500000 is tried as well.

I’ve included the loop-code:

void loop() {
  while (i2cRead(0x3B, i2cData, 14));
  accX = (int16_t)((i2cData[0] << 8) | i2cData[1]);
  accY = (int16_t)((i2cData[2] << 8) | i2cData[3]);
  accZ = (int16_t)((i2cData[4] << 8) | i2cData[5]);


#ifdef RESTRICT_PITCH 
  double roll  = atan2(accY, accZ) * RAD_TO_DEG;
#else // Eq. 28 and 29
  double roll  = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
#endif

  Serial.print(roll); Serial.print("\t"); // ANGLE!
  Serial.print("\r\n");
  delay(2); // if this is below 2 ms (approximately 500 Hz), serial monitor starts lagging (only at -xx.xx, not at -x.xx or at xx.xx)
}

In order to make it easy to test please upload a sketch that simply prints a range of values that illustrates the problem. Turning on the timestamps in the Serial monitor will provide timing information

If there is no problem with the simple sketch then the problem is not with printing and is elsewhere in th sketche

I've tried a simple sketch:

double test = -45.00;

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

void loop() {
  Serial.println(test);
  test += 0.01;
  delay(2);
  
  if (test > 45.00) test = -45.00;
}

The serial monitor lags/stutters if the delay is 1.99 ms and below (all numbers, not not -xx.xx), no lag if the delay is 2 ms. Several baud rates tried: 115200, 230400.

This is copied from the monitor at 1.99 ms delay (lag):
12:44:07.964 -> -45.00
12:44:07.964 -> -44.99
12:44:07.964 -> -44.98
12:44:07.964 -> -44.97
12:44:07.964 -> -44.96
12:44:07.964 -> -44.95
12:44:07.964 -> -44.94
12:44:07.964 -> -44.93
12:44:08.626 -> -44.92
12:44:08.626 -> -44.91

This is copied from the monitor at 2 ms delay (no lag):
12:45:08.226 -> -44.99
12:45:08.226 -> -44.98
12:45:08.226 -> -44.97
12:45:08.226 -> -44.96
12:45:08.226 -> -44.95
12:45:08.226 -> -44.94
12:45:08.226 -> -44.93
12:45:08.226 -> -44.92
12:45:08.226 -> -44.91
12:45:08.226 -> -44.90
12:45:08.274 -> -44.89
12:45:08.274 -> -44.88

The serial monitor lags/stutters if the delay is 1.99 ms

delay() takes un unsigned long int as its parameter so no surprise if you have problems if you use 1.99

My bad. That was a serious brainfart. The delay() is now exchanged for delayMicroseconds.

I still have the same problem though. The monitor is lagging at 1000 Hz (but now, the monitor can handle a loop-frequency of 588 Hz (1700 us delay)

The first thing to try is to increase the baud rate. The Serial monitor will run at up to 2000000 baud

I have tested with different data types. 1400 us (714 Hz) is achievable using int instead of double (2 bytes vs 4 bytes).

I've tried to increase baud rate with no luck. This shouldn't be necessary anyways, since 1000 doubles per second is 4 kB/s, equaling a necessary baud rate of 32768 (4000 * 8 = 32000)

I've tried to increase baud rate with no luck. This shouldn't be necessary anyways, since 1000 doubles per second is 4 kB/s, equaling a necessary baud rate of 32768 (4000 * 8 = 32000)

More like 40000 bits per second because of the start and stop bits, probably a bit more than 40000 because to get 40000 there would have to be no gaps between bytes. I think I would assume 50000 bits per second to give a bit of room.

I would also doubt that a Nano can shovel bytes around that fast.