LCD Calculator Code

Hi everyone i wrote some code i don't know why its not working

#include <LiquidCrystal.h>

int rs = 7;
int e = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);

int n1;
int n2;
String msg;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {
  while (Serial.available() == 0) {
  }
  n1 = Serial.parseInt();
  n2 = Serial.parseInt();
  msg = Serial.readStringUntil('\n'); // Read until newline character

  Serial.print("Received: ");
  Serial.print(n1);
  Serial.print(" ");
  Serial.print(n2);
  Serial.print(" ");
  Serial.println(msg);

  if (msg == "+") {
    int result = n1 + n2;
    displayResult("Addition", n1, msg, n2, result);
  } else if (msg == "-") {
    int result = n1 - n2;
    displayResult("Subtraction", n1, msg, n2, result);
  } else if (msg == "*") {
    int result = n1 * n2;
    displayResult("Multiplication", n1, msg, n2, result);
  } else if (msg == "/") {
    if (n2 != 0) {
      float result = (float)n1 / n2;
      displayResult("Division", n1, msg, n2, result);
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Error: Division by 0");
    }
  }
}

void displayResult(String operation, int operand1, String op, int operand2, float result) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(operation);
  lcd.setCursor(0, 1);
  lcd.print(operand1);
  lcd.print(op);
  lcd.print(operand2);
  lcd.print("=");
  lcd.print(result);
  delay(2000);
  lcd.clear();
}

Welcome to the forum

More details please

What should it do ?
What does it do ?
Which Arduino are you using ?
What is Line ending set to in the Serial monitor ?

elegoo uno r3

bro I'm just trying to do simple calculator with elegy uno r3 LCD is working when i print something else but when i tried this code its not working

9600

Not the baud rate, the Line ending

its no line ending

Answers please

So is the

Serial.readStringUntil('\n');

ever going to find a Newline character in the input ?

yes

it s just calculator we are just writing numbers and operation sign then its printing on the LCD
like that 3 5 * ----- it will be 3 * 5 =15

I am about to give up

what do you mean what does it do ?

You said

So what does it do when you enter numbers or commands ? Is anything printed on the LCD for example or is the calculation wrong ? What ?

yes its shows something when i entered something else but when i tried this code its receives inputs and printing them not he serial monitor not printing them on the LCD

Start by setting the Line ending to New Line in the Serial monitor to avoid the need to wait of the Serial timeout to occur

Are you sure that the baud rate of the sketch and the Serial monitor match ?

Try this code:

#include <LiquidCrystal.h>

int rs = 7;
int e = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);

int n1;
int n2;
String msg;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {
  while (Serial.available() == 0) {
  }
  n1 = Serial.parseInt();
  n2 = Serial.parseInt();
  msg = Serial.readStringUntil('\n'); // Read until newline character
  Serial.print(msg[0], HEX); //  First digit after last number
  Serial.print(" ");
  Serial.print(msg[1], HEX); //  2o. digit after last number
  Serial.print(" ");
  Serial.println(msg[2], HEX);//  May be 3o. digit after last number

  Serial.print("Received: ");
  Serial.print(n1);
  Serial.print(" ");
  Serial.print(n2);
  Serial.print(" ");
  Serial.println(msg);

  if (msg[1] == '+') {
    int result = n1 + n2;
    displayResult("Addition", n1, msg, n2, result);
  } else if (msg[1] == '-') {
    int result = n1 - n2;
    displayResult("Subtraction", n1, msg, n2, result);
  } else if (msg[1] == '*') {
    int result = n1 * n2;
    displayResult("Multiplication", n1, msg, n2, result);
  } else if (msg[1] == '/') {
    if (n2 != 0) {
      float result = (float)n1 / n2;
      displayResult("Division", n1, msg, n2, result);
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Error: Division by 0");
    }
  }
}

void displayResult(String operation, int operand1, String op, int operand2, float result) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(operation);
  lcd.setCursor(0, 1);
  lcd.print(operand1);
  lcd.print(op);
  lcd.print(operand2);
  lcd.print("=");
  lcd.print(result);
  delay(2000);
  lcd.clear();
}

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