/*
Arduino | coding-help
AmBored — 7:00 PM
Hey im trying to program a calculator and display it on my lcd
but the code isnt calculating properly and i cant debug the problem.
Do you guys know what i missed?
*/
#include <LiquidCrystal.h>
const int rs = 12;
const int e = 11;
const int D4 = 10;
const int D5 = 9;
const int D6 = 8;
const int D7 = 7;
LiquidCrystal lcd(rs, e, D4, D5, D6, D7);
String Operator ;
float firstnumber ;
float secondnumber ;
float result ;
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:
lcd.setCursor(0, 0);
lcd.print("Enter your 1st");
lcd.setCursor(0, 1);
lcd.print("number: ");
Serial.println("Enter your first number") ;
while (Serial.available() == 0) {
}
firstnumber = Serial.parseFloat() ;
lcd.print(firstnumber);
delay(1000);
lcd.clear() ;
lcd.setCursor(0, 0);
lcd.print("Enter your 2nd");
lcd.setCursor(0, 1);
lcd.print("number");
Serial.println("Enter your second number") ;
while (Serial.available() == 0) {
}
secondnumber = Serial.parseFloat() ;
lcd.clear() ;
lcd.setCursor(0, 0);
lcd.print("Pick an operator");
lcd.setCursor (0, 1) ;
lcd.print("- + * / ") ;
Serial.println("Pick an operator") ;
while (Serial.available() == 0) {
}
Operator = Serial.readString() ;
if (Operator == "-") {
result = firstnumber - secondnumber ;
}
if (Operator == "+") {
result = firstnumber + secondnumber ;
}
if (Operator == "*") {
result = firstnumber * secondnumber ;
}
if (Operator == "/") {
result = firstnumber / secondnumber ;
}
lcd.clear() ;
lcd.setCursor(0, 0) ;
lcd.print("Your answer is") ;
lcd.setCursor(0, 1) ;
lcd.print(result) ;
delay(1000) ;
Serial.print("Your answer is ") ;
Serial.println(result, 4);
lcd.clear() ;
}