I'm following a guy on YT who gave us/me a homework assignment to build a simple calculator. You enter numbers and operator via the serial monitor and also display on LCD. My "calculator" skips the if statement asking is the user entered a "+". I just can't figure out why. Thoughts?
Best,
Tony
#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int firstNum;
int secondNum;
int result;
String operator1;
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:
Serial.println("Enter 1st number");
lcd.setCursor(0,0);
lcd.print("Enter 1st number");
while (Serial.available()==0) {
}
firstNum = Serial.parseInt();
lcd.clear();
Serial.println("Enter 2nd number");
lcd.setCursor(0,0);
lcd.print("Enter 2nd number");
while (Serial.available()==0) {
}
secondNum = Serial.parseInt();
lcd.clear();
Serial.println("Enter operator");
lcd.setCursor(0,0);
lcd.print("Enter operator");
while (Serial.available()==0) {
}
operator1 = Serial.readString();
lcd.clear();
if (operator1 == "+") {
result = (firstNum + secondNum);
lcd.setCursor(0,0);
lcd.print(firstNum);
lcd.print(" ");
lcd.print(operator1);
lcd.print(" ");
lcd.print(secondNum);
lcd.print(" ");
lcd.print("=");
lcd.print(" ");
lcd.print(result);
}
//
//if (operator1 = "-") {
//result = (firstNum - secondNum);
//}
//
//if (operator1 = "*" || operator1 = "x" || operator1 = "X") {
//result = (firstNum * secondNum);
//}
//
//
//if (operator1 = "/") {
//result = (firstNum / secondNum);
//}
//
//
Serial.print(firstNum);
Serial.print(" ");
Serial.println(secondNum);
Serial.print(" ");
Serial.print(operator1);
//
//
}