5 Serial Monitor Questions (flush, available etc.)

I have a few questions about serial.

My objective is to

  1. when the Void Setup () is called in the beginning I would like for it to discard anything it has saved from the previous run that did not get to be displayed. I imagine the code would look something like this, but I have not been able to get this to work.

Void setup()
{
Serial.begin(9600);
Serial.available() =0) ; // Clear out any leftovers
}

Can something like this be done? I want whatever is in the serial.buffer to disappear before any new data comes in to the serial monitor.

  1. I have noticed the serial monitor completely stopped before. This is different than the typical skip letters. Is there a limit on what the serial monitor can print out and remember? Like how far can you scroll on the serial monitor and look up history? There obviously has to be a limit, but is there a way to make it so that it starts erasing that history when it gets full?

  2. I have read Robin2's tutorial on serial input basics as well as a few pages from Surfer Tims website (which is very detailed) but I do not understand how to make the added Serial.flush functions in the mega work.

Arduino Mega only:
Serial1.flush()
Serial2.flush()
Serial3.flush()

Like for example, is this designed for if you have three lines of code that print to the serial monitor. You can say Serial.1 you print the first line and don't leave until you are finished. Serial.2 you print the second, and Serial.3 you print the third line. This way nothing is skipped?

  1. Does anyone happen to have any examples of using multiple Serial.flushes?
  1. Good news. You don't need to do anything. When the board is reset anything that was left in the serial buffer is gone.

Here is the code that I just ran. It is a simple sketch that came pre-loaded in the libaray. All I added was Serial.println("Startup complete"); in the void setup() code so that you would see.

/*
  Analog input, analog output, serial output
 
 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.
 
 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground
 
 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  Serial.println("Setup complete");
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);                     
}

Now take a look at the attachment and you will see there is some leftover code up at the top. I want this to not happen.

  1. I've never seen it stop and I've run a bunch of stuff out on it. Can you post some code that causes this?

Sure, i'll look for it tomorrow.

What do you want Serial.flush to do for you?

I want the program to print out data strings. I'm trying to save some stuff to a sd card, and have noticed sometimes the sd card doesn't save, and it gets weird. It's pretty much the same thing you will see in the serial monitor sometimes when it skips things.

Setup.gif

It's a Java program running in Windows, dependent on USB drivers and USB plugs. Garbage happens, like the plug can momentarily lose the USB connection and it doesn't resume because the reconnected USB is a new session.

The incorrect data at the start comes from the Arduino being reset every time the USB connection is made. The serial monitor can actually see a few characters that were sent prior to the reset. With a simpler serial system, you wouldn't see that.

Now if it's the other way around - the Arduino needs to ignore some characters, you can code for that. Assume you have some setup, like initializing an LCD screen that takes some time and you receive some data during that time which you wish to discard. (Like from a GPS or some other machine.)

while(Serial.available()) Serial.read(); //discard everything in the serial input buffer

I know it is a little untidy, but does it really matter if there is some garbage in the Serial Monitor.

If you were writing a PC program to receive data from the Arduino you would need to deal with this and this Python demo shows how

...R

Now take a look at the attachment and you will see there is some leftover code up at the top. I want this to not happen.

Is the program you posted in reply #2 the one that produced the output in the attachment ? If so then I don't see how, because it has only one line in it that outputs to the serial monitor.

It does have these lines

 // print the results to the serial monitor:
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);

but no code to do what they say.