Arduino Serial Port Prints Old Values

Hi,

Oftentimes when I am printing data to the serial port, the Arduino prints values from a previous sketch (at least that's what I think is happening, the buffer is too full I suppose) before it starts printing the new data. So, let's say for instance I am printing two values that are comma separated. The first value is in the range of 0.2-0.4 and the second value is 70 followed by a new line character. So, whenever I open the serial port, the Arduino will print some data like this

0.2,70
0.21,70
0.22,70
0.23

Notice the last value is not accompanied by the comma and the second value. This makes me think that the Arduino is printing "old data" and that the buffer was just really full.

Then, the Arduino starts printing the new values from the current sketch.

0.20,70
0.21,70
0.22,70
0.23,70
0.24,70

My code is posted below. This is the specific program I am working on right now, but this problem occurs whenever I print serial data and open the serial port.

Any help would be appreciated, thanks.

const double x_initial = 0.2;
const double x_max = 0.4;
const double x_increment = 0.01;

double x = x_initial;

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


void loop()
{
  x += x_increment;
  
  Serial.println(x,5);
  Serial.print(",");
  Serial.println(70);

  if (x >= x_max-x_increment)
  {
    x = x_initial-x_increment;
  }
}

You have 3 Serial.print statements within your loop function that runs upwards of about 50,000 times per second. Are you seriously expecting it to keep up?

Ah okay got it. Thanks.

You're welcome.

Are you seriously expecting it to keep up?

Why not? When the buffer gets full, Serial.print() blocks until there is room. 50,000 iterations per second just went out the window.

PaulS:
Why not? When the buffer gets full, Serial.print() blocks until there is room. 50,000 iterations per second just went out the window.

I think I may be getting missled by this.

Programming Tips

As of version 1.0, serial transmission is asynchronous; Serial.print() will return before any characters are transmitted.

I assume that asynchronous means it will have to drop the overflow. Unfortunately I'm not in a position to test this.