Serial.available doesnt work

Board: Elegoo UNO R3
Main Components: LCD Display and Potentiometer

I have attached the whole file.
I am a beginner and this question is regarding Serial.available command.

I have used it 3 times in the code, it works the 1st and the 3rd time. Not the 2nd. That is weird in my opinion.

This might not be related, but wirings are not an issue as just before making this calculator using LCD to display the result, I did the basic Hello World display on LCD and it worked fine.

The Serial Monitor waits until the first number is entered. As soon as the first number is entered, it prints the message to enter the second number, and almost immediately after that the message to choose the operator is printed. That leaves no time to enter the second number. It will wait after the message to choose the operator is displayed.

Please also suggest me where I can improve the code or effective methods to achieve same results.

Here is the code:

#include <LiquidCrystal.h>

int rs = 7;
int en = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
int dt = 1500;
float n1 = 0;
float n2 = 0;
int op;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16,2);

}

void loop() {
  // put your main code here, to run repeatedly:

//Get the first Number:
  
Serial.println("Enter first number:");
while(Serial.available()==0){} // wait for the user input
n1 = Serial.parseFloat();

//Get the second Number:

Serial.println("Enter second number:");
while(Serial.available()==0){} // wait for the user input
n2 = Serial.parseFloat();

//Get the Operator:

Serial.println("Please enter the operator:");
Serial.println("'1' for addition,");
Serial.println("'2' for subtraction,");
Serial.println("'3' for multiplication,");
Serial.println("'4' for division");
while(Serial.available() == 0){} //wait for the user input
op = Serial.parseInt();

//Display first number on the LCD:

lcd.setCursor(0,0);
lcd.print("1st Number:");
lcd.setCursor(0,1);
lcd.print(n1);
delay(dt);
lcd.clear();

//Display second number on the LCD

lcd.setCursor(0,0);
lcd.print("2nd Number:");
lcd.setCursor(0,1);
lcd.print(n2);
delay(dt);
lcd.clear();

//Performing the calculation

if(op == 1)
{
  lcd.setCursor(0,0);
  lcd.print("Your answer:");
  lcd.setCursor(0,1);
  lcd.print(n1);
  lcd.setCursor(1,1);
  lcd.print("+");
  lcd.setCursor(2,1);
  lcd.print(n2);
  lcd.setCursor(3,1);
  lcd.print("=");
  lcd.setCursor(4,1);
  lcd.print(n1+n2);
}
else if(op == 2)
{
  lcd.setCursor(0,0);
  lcd.print("Your answer:");
  lcd.setCursor(0,1);
  lcd.print(n1);
  lcd.setCursor(1,1);
  lcd.print("-");
  lcd.setCursor(2,1);
  lcd.print(n2);
  lcd.setCursor(3,1);
  lcd.print("=");
  lcd.setCursor(4,1);
  lcd.print(n1-n2);
}
else if(op == 3)
{
  lcd.setCursor(0,0);
  lcd.print("Your answer:");
  lcd.setCursor(0,1);
  lcd.print(n1);
  lcd.setCursor(1,1);
  lcd.print("x");
  lcd.setCursor(2,1);
  lcd.print(n2);
  lcd.setCursor(3,1);
  lcd.print("=");
  lcd.setCursor(4,1);
  lcd.print(n1*n2);
}
else if(op == 4)
{
  lcd.setCursor(0,0);
  lcd.print("Your answer:");
  lcd.setCursor(0,1);
  lcd.print(n1);
  lcd.setCursor(1,1);
  lcd.print("/");
  lcd.setCursor(2,1);
  lcd.print(n2);
  lcd.setCursor(3,1);
  lcd.print("=");
  lcd.setCursor(4,1);
  lcd.print(n1/n2);
}
else
{
  Serial.println("Please choose the proper format");
}

lcd.clear();

}

I thank everyone in advance!

Calculator.ino (2.52 KB)

Is your serial monitor line ending set to NONE?

Please edit your post to include the code, within code tags. People using smartphones can't see it when appended.

Explain what "doesn't work" means. Tell us what you expect to happen and what happens instead.

Hing: make sure that you have the appropriate line ending set on the serial monitor. I'm going to guess that you want "no line ending" since your code does not expect to see any.

Don't you see a difference between these two lines ?

First number :

while(Serial.available()==0){} // wait for the user input

Second number :

while(Serial.available()){} // wait for the user input

JCA34F:
Is your serial monitor line ending set to NONE?

I don't have "NONE" option for the "Line ending tab" of my Serial Monitor of IDE1.8.13.

GolamMostafa:
I don't have "NONE" option for the "Line ending tab" of my Serial Monitor of IDE1.8.13.

And you're running completely different code. So do you have any helpful comment to make which is relevant to the original question?

Steve

@GolamMostafa:

Is it conceivable to you that "line ending set to NONE" and "no line ending" mean the same thing?

@GolamMostafa

mine does not show None either :slight_smile:
menu.png
I did understand what was suggested though... (ie not sure what was the value of your answer there)

menu.png

@GolamMostafa might have made the point differently, but s/he does have a point.

While a small step for man, it might be a giant leap for mankind. Yes, there are people here who must strike us as dumber than a bag of hair. Who would sit for hours and never make a connection between what someone told them explicitly to look for (“NONE”) and an available option all you geniuses spot instantly as meaning the same thing.

a7

Since @mrunang is MIA we don't know.
I would expect OP to ask what they don't understand

@GolamMostafa could probably have added a smiley to make his point

Few days ago the member of this Forum with longest user name advised a poster to choose "NONE" option for the "Line ending tab" of the Serial Monitor. When the following picture (Fig-1) was brought to the kind attention of that member, he politely agreed to review his advice.

lineEnd.png
Figure-1:

@JCA34FI is an exception!

The official language of Arduino Forum is English. The options for the Line ending tab are given in clear English. If someone writes a tutorial for me, then I would expect to have lessons in check-list format; where, I must find exactly what is being told in the check list.

lineEnd.png

I would expect to have lessons in check-list format; where, I must find exactly what is being told in the check list.

Murphy's Law guarantees that very shortly after someone has taken the time to carefully compile the instructive "check-list" and post it for you, the original document or program will be modified by someone else and reposted, invalidating the check list.

jremington:
Murphy's Law guarantees that very shortly after someone has taken the time to carefully compile the instructive "check-list" and post it for you, the original document or program will be modified by someone else and reposted, invalidating the check list.

But it is also guaranteed that the Engineer's Hand Book is not affected by Murphy's Law.

guix:
Don't you see a difference between these two lines ?

First number :

while(Serial.available()==0){} // wait for the user input

Second number :

while(Serial.available()){} // wait for the user input

I was trying to see if I removed the zeros it would work or not. The first time I wrote the code, I has the zero included.
Now that I changed the newline to no line ending, it works.
Thank you again.

J-M-L:
Since @mrunang is MIA we don't know.
I would expect OP to ask what they don't understand

@GolamMostafa could probably have added a smiley to make his point

Its clear now, all I had to do was to change the newline to no line ending, and it works.
Thnx
Mrunang

alto777:
@GolamMostafa might have made the point differently, but s/he does have a point.

While a small step for man, it might be a giant leap for mankind. Yes, there are people here who must strike us as dumber than a bag of hair. Who would sit for hours and never make a connection between what someone told them explicitly to look for (“NONE”) and an available option all you geniuses spot instantly as meaning the same thing.

a7

Anyone else old enough to remember the DOS prompt

Press any key to continue . . .

When I was taking computer classes back in the dark ages, at the start of the year there always seemed to be at least one person in class who would be searching the keyboard for a key labeled "any". There was a similar problem where some keyboards used "Enter" and others used "Return".

I remember "Keyboard not found, press F1 to continue" which I guess that dates me

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.