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)