How do I print multi digit numbers on a Lcd_i2c

33 views Asked by At

I am making an "operating system" for My Arduino Uno Where you can enter and exit "programs" And I am making a calculator "program" Where you can Type in the numbers you want to add Using the serial monitor And it will show your output on the lcd_i2c Screen(16x2) Although I am having issues printing multi digit numbers To the lcd_i2c screen here is my code

#include <Arduino.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); 

char input;
char ch;
int num1, num2, num3;

void setup()
{
  Serial.begin(9600);
  lcd.init();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("UNO OS v1.0.0");
  lcd.setCursor(0,1);
  lcd.print("pro: Cal+(+)");
}


void loop()
{
  if (Serial.available() > 0)
  {
    input = Serial.read();
    if (input == 'q')
  {
  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("UNO OS v1.0.0");
  lcd.setCursor(0,1);
  lcd.print("pro: Cal+(+)");
  }
  if (input == '?')
  {
    Serial.println("If this is the only command that works for you your Microcontroller is not supported Currently supported microcontroller (Atmega328p)");
  }
  
    if (input == '+')
    {
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.print("Cal+ v0.0.1");
      delay(4000);
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.print("number1:");
      while (Serial.available() == 0){

      }
      num1 = Serial.read() - '0';
      lcd.clear();
      lcd.print("number2:");
      while (Serial.available() == 0){

      }
      num2 = Serial.read() - '0';
      lcd.clear();
      lcd.print("adding...");
      delay(3000);
      lcd.clear();
      num3 = num1 + num2;
      lcd.print("your number is:");
      lcd.print(num3);
    } 
  }
}

Initially When it printed any number it would print the ASCII code for the character for example 1 in ASCII code is 49 I Partially fixed the issue by subtracting num1 and num2 by 0 So then I will get that single digit number (In ASCII Code this is what it looks like If I wanted to do one plus one the first input Would be put into the variable num1 And then num1 Would be subtracted by zero in ASCII 49(1) - 48(0) = 1 And the same thing for my next input) I thought I fixed my issue but it only fixed printing single digit numbers and it did not fix printing multidigit numbers so printing anything beyond nine Would only show the first digit of that multi digit number

0

There are 0 answers