While (Serial.available() ==0) {} issue, Please HELP !

That code did not produce the posted output.

I am amazed by the way you pointed out my mistake so honestly and sincerely, without showing even the slightest sign of annoyance. In return, I have only to say -- thank you very much!

You are absolutely correct. The error was caused by my careless copy-and-paste operation. A significant portion of the following line, both at the beginning and at the end, was inadvertently omitted:

 Serial.print(Serial.read()); Serial.print("  ");

I am extremely sorry for the inconveninnce.

The corrrect sketch is here:

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

void loop()
{
  while (Serial.available() > 0)
  {
    delay(3000);
    Serial.print(Serial.read()); Serial.print("  ");
    Serial.println(Serial.available());
    
  }
}

of course seeing the code woudl make this clearer, but it sounds like you need a state machine or sequencer to control things rather than multiple while loops keep things in check

events include a timer used for changing flow-rates at specified periods, monitoring serial input as well as buttons.

states might include Idle Run, DumpLog, ...

Serial inputs can include manual commands to control flow rate as well as flow rates and period.

To set the record straight for anyone tripping over this thread in the future, here are the four output line ending possibilities from Serial Monitor for a single line transmission:

49 6
50 5
51 4
52 3
53 2
13 1
10 0
------//that was 12345<Both NL and CR>
49 5
50 4
51 3
52 2
53 1
13 0
------//that was 12345<Carriage Return>
49 5
50 4
51 3
52 2
53 1
10 0
------//that was 12345<Newline>
49 4
50 3
51 2
52 1
53 0
------//that was 12345<no line ending>

N.B. items demarked // in above were added here, not printed by the program, and indicate the setting for line ending in Serial Monitor.

The above was produced by the following:

void setup() {
  Serial.begin(9600);  //approx 1 char per ms
}

void loop() {
  if (Serial.available() > 0) {
    delay(10);    //to accumulate all the characters in buffer, presuming < 10 @9600 in a single transmission; math is Back-Of-The-Envelope, not rigorous
    while (Serial.available() > 0) {
      Serial.print(Serial.read(),DEC);  //shows us the actual integer value of each received character
      Serial.print(' ');  //formatting for clarity
      Serial.println(Serial.available());  //how many still to process
    }
    Serial.println("------");
  }
}

In the IDE, it comes as: Both NL & CR. So, during transmission character code for CR (Carriage Return) is transmitted first follwed by NL (New Line).

Newline and Linefeed are pretty much synonymous. Depends on how old you are; if you remember teletypes, you won't get confused by this - the line advanced one row, but the head did not return to the left unless CR was received(Carriage Return).

However, I sense you're just trolling, as usual. Carry on, I'm out of here.

Why is presuming? It is certain that all the seven charcaters would arrive in less than 10 ms. My calculation gives ~= 7.3 ms.

You are doing injustice to me without trying to understand my true intention. I have tried to address a techncal issue in respect of accuracy and consistency rather than trolling. In the IDE, there is no LF but NL!

In good old days,
LF (Line Feed): The head (of the type writer) comes back to home position and then the paper goes one line up. Here, we have two operations: Carraige Return and New Line.

Nowadays:
New Line: I have no idea how Aduino has implemented it in IDE as New Line has a single character code -- 0x0A. It brings the cursor at the left-most position of the screen and then goes one line down. Here, we have two operations.

Post #24 edited to reflect actual Serial Monitor menu wording.

@GolamMostafa Post #24 was a simple attempt to clean up the fuzzy and incomplete state you had left this thread in. My apologies for my failure to use the exact wording in Serial Monitor. I am now returning you to "Ignored" status.

As a non-native English speaker, I sometimes find it difficult to understand the meaning of idiomatic or phrasal expressions.

For the phrase above, Google provided five possible interpretations, yet I am still unable to determine which one best fits the context.

"LF" is a familiar term to many people from the manual typewriter era, particularly those of our generation (71+ years old).

I commissioned this elaborate but simple sketch that shows the buffer filling rapidly as characters arrive from the serial monitor, and emptying slowly as they are removed on a timed basis.

Try it here

The code
# include <Adafruit_NeoPixel.h>

/*
  Serial Buffer Ring Demo

  Green  = bytes waiting in the serial receive buffer
  Yellow = byte position just consumed

  Try Serial Monitor at 300 baud to watch the buffer fill visibly.
*/

const byte LED_PIN = 6;
const byte LED_COUNT = 32;

const unsigned long CHARACTER_INTERVAL_MS = 333;
const unsigned long YELLOW_MS = 100;

Adafruit_NeoPixel ring(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

unsigned long lastCharacterTime = 0;
unsigned long yellowUntil = 0;

byte readPixel = 0;
byte yellowPixel = 0;

void showBufferLevel(int available)
{
  unsigned long now = millis();

  ring.clear();

  int shown = min(available, LED_COUNT);

  for (int i = 0; i < shown; i++)
  {
    byte pixel = (readPixel + i) % LED_COUNT;
    ring.setPixelColor(pixel, 0x00ff00);   // green
  }

  if (now < yellowUntil)
  {
    ring.setPixelColor(yellowPixel, 0xffff00); // yellow
  }

  ring.show();
}

void setup()
{
  Serial.begin(300);

  while (!Serial)
  {
    ;
  }

  ring.begin();
  ring.clear();
  ring.show();

  Serial.println();
  Serial.println("serial demo\n");

if (0) {
  Serial.println("Paste text. Green shows waiting bytes.");
  Serial.println("Yellow marks the byte just consumed.");
  Serial.println("Bytes are s l o w l y read");
  Serial.println();
}
}

void loop()
{
  unsigned long now = millis();

  if (Serial.available() > 0 &&
      now - lastCharacterTime >= CHARACTER_INTERVAL_MS)
  {
    int ch = Serial.read();

    yellowPixel = readPixel;
    yellowUntil = now + YELLOW_MS;

    readPixel = (readPixel + 1) % LED_COUNT;
    lastCharacterTime = now;

    Serial.print("# = ");
    Serial.print(Serial.available());
    Serial.print("'");

    if (ch >= 32 && ch <= 126)
      Serial.write((char)ch);
    else
      Serial.print('?');

    Serial.println("'");
  }

  showBufferLevel(Serial.available());
}

a7

Good work excpet that you have shown a ring buffer in the shematic. Is our serial buffer a ring one or a linear one?

Yeth, good eye. We had a simple bar graph display showing the number of characters in the buffer. That one would shoot up and crawl down.

The ring does behave like a circular buffer, and just made for a more interesting display; while the last string is being doled out slowly, you can enter a new string and see it go into the ring. Characters briefly flash yellow as they are removed.

Since the underlying mechanism for serial input buffering is such a ring, I think with some hand-waving the ring display can claim to be mimicking the real buffer.

With more control over the graphics, the ring could show the actual character values waiting.

IDK if it might be educational in the right circumstances. What I can say is that playing with it passes for fun in my empty life. :expressionless:

a7

Your ring buffer could be mde more refined by making its size exactly 64.

If you really want to mess around, the Serial code is, after all, not a secret; add a couple of calls to provide the pointers for receive and transmit buffers (4 calls, 4 pointers total), and go nuts tracking your actual serial performance. Very little code added to Serial, no impact to normal performance, and hours of fun... Be sure to make the LED strings identical in length to the respective buffers. Run the whole serial interface at as low a baud rate as Serial Monitor, or your Arduino and another Terminal program, will go.

{walks away, snickering, thinking about the black hole @alto777 has wandered into}