While (Serial.available()==0) is not waiting

Hello
I wantaed to take the name and phone number of clients and store it on an array but while (Serial.available()==0) is not waiting me. It goes toes to another execution before I give any input. What could be the problem? here is my code

void Regular(){
  Serial.println(F("case R"));
  Serial.print(F("name: "));
  while (Serial.available()==0){
  } 
  regular[n][1]=Serial.readString();
  Serial.println(regular[n][1]);  
  Serial.print(F("phone_num: "));
  while (Serial.available()==0){
  } 
  regular[n][2]=Serial.readString();
  Serial.println(regular[n][2]);  
}

I can't compile your code.
It's incomplete.

It would not do this if the serial buffer was empty. So there are some characters from the previous input in the buffer. Do you work with serial before this function?

Do not show a snippets, always show a complete code.

If the Serial input is coming from the Serial monitor then what is Line ending set to ?

Anything other than "No line ending" will cause extra characters to be sent when you press Return to send the string

String regular[6][4];
unsigned int n=1;
void setup() {
Serial.begin(9600);
}
void Regular(){
  Serial.println(F("case R"));
  Serial.print(F("name: "));
  while (Serial.available()==0){
  } 
  regular[n][1]=Serial.readString();
  Serial.println(regular[n][1]);  
  Serial.print(F("phone_num: "));
  while (Serial.available()==0){
  } 
  regular[n][2]=Serial.readString();
  Serial.println(regular[n][2]);  
 n++;
 delay(1000);
}
void Case(){
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case 'r':
      Regular();     
      break;
}}
void loop(){
Case();
}

What is the line ending used when you sending the 'r' from Monitor?

1 Like

It goes into "phone num" before i type any names

Read @b707's post... and I think you will find the answer.

You have a line ending set in the serial monitor of something other than "No line ending"... so this statement...

reads the character input ('r')... BUT it does not read the line ending character(s)... so when you then use...

This is immediately false... because there are CR of LF characters waiting to be read.

Solution... turn off line ending characters is the serial monitor.

I used both NL and CR does this have a problem also?

.... or flush all waiting bytes from the buffer after reading the 'r'

Yes, of course. 2 extra characters will be added to the input

Set the Line ending to "No line ending"

Ok I have set it into no line ending and it works, but how can i flush all waiting bytes from the buffer

while (Serial.available() > 0)
{
  Serial.read();   //read available bytes and ignore them
}

I have tried it and it is not working for me

Yes... because what you are telling the serial monitor is to add some characters onto what you type... if you do this you need to handle those extra characters. Easiest solution is to use "No line ending". 2nd easiest... as @b707 has suggested.. after you get what you want, flush everything else the buffer before waiting for the next character.

Once you edited the code and got a new errors (issues)- always show the revised code

Sory i forgot to change it into no line ending mode, it is working now

Thank you Guys

... sigh.

3 Likes

I did change it but then i changed it back just to make sure and then i forget to change it back