Whileloop not waiting for second input, assuming 0

Hi,

I am currently playing around with the arduino and decided to build a calcuatlor with an LCD screen. The code is very straight forward.

The problem is, rather than wait for the second number to be inputted to the serial monitor, it is assuming a ‘0’ and the second while(==0) is being skipped.

Any help would be great.

code below;

#include <LiquidCrystal.h>

int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;

float firstNum;
float secNum;
float answer;

String op;

LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

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

void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print(“Input Number”);
while(Serial.available()==0)
{

}
firstNum=Serial.parseFloat();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Input 2nd Number”);

while(Serial.available()==0)
{

}
secNum=Serial.parseFloat();

lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Op(+,-,*,/)");

while(Serial.available()==0)
{

}
op=Serial.readString();

if(op=="+")
{
answer=firstNum+secNum;
}
if(op=="-")
{
answer=firstNum-secNum;
}
if(op=="")
{
answer=firstNum
secNum;
}
if(op=="/")
{
answer=firstNum/secNum;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(firstNum);
lcd.print(op);
lcd.print(secNum);
lcd.print("=");
lcd.print(answer);
// lcd.setCursor(0,1);
// lcd.print(“thank you”);
delay(5000);
lcd.clear();

Calc_LCD.ino (1.25 KB)

Hello scrandells,
Welcome to the Arduino fora.

Before you do anything else please take a moment to read General guidance and
How to use this forum
Especially item #7 on posting code.

On the bottom of the serial monitor look for ‘line ending’, what is is set to? Set it to ‘no line ending’.

Like this:

Thanks, that was the issue!

I read over the guidelines for posting code as well.

Cheers,

Scrands

scrandells:
Thanks, that was the issue!

Do you understand why?

PerryBebbington:
Do you understand why?

yes, becasue my serial monitor was sending an ASCII new line code after i entered it. Where as the no line ending was sending only what i entered in to console

OK, good :slight_smile: