anshul69
Published

Keypad Calculator using LCD

Ever wished to make your own calculator? This is how you can!

IntermediateProtip3,308
Keypad Calculator using LCD

Things used in this project

Story

Read more

Schematics

Schematic Diagram

Circuit Diagram

Code

Untitled file

Arduino
#include <Keypad.h>
#include <LiquidCrystal.h>
int Contrast = 120;
LiquidCrystal lcd(A1, A2, 13, 12, 11, 10);

const byte ROWS = 4; 
const byte COLS = 4; 
int expr[2];
int i=0;
char op;

void resetCalc()
{
  	Serial.println();
  	i = 0;
  	lcd.clear();
  	lcd.setCursor(0,0);
}
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
void setup(){
  Serial.begin(9600);
  analogWrite(A0, Contrast);
  lcd.begin(16, 2);
  lcd.noCursor();
  lcd.setCursor(0,0);
  Serial.println("Welcome to keypad Calculator");
  Serial.println("A = +, B = -, C = *, D = /");
  Serial.println("Press # to calculate");
  Serial.println("Press * to reset");
  Serial.println("NOTE: It works only on single digit numbers\n\n");
}
  
void loop(){
  char key = customKeypad.getKey();
  if(key)
  {
    if(i == 0)
    {
          expr[0] = key-48;
          Serial.print(expr[0]);
      	  lcd.print(expr[0]);
          i++;
    }
    else if(i == 1)
    {
          op = key;
      	  i++;
     	  if(op == 'A')
          {
          	Serial.print(" + ");
            lcd.print(" + ");
          }
      	  else if(op == 'B')
          {
          	Serial.print(" - ");
            lcd.print(" - ");
          }
          else if(op == 'C')
          {
          	Serial.print(" * ");
            lcd.print(" * ");
          }
          else if(op == 'D')
          {
          	Serial.print(" / ");
            lcd.print(" / ");
          }
          else
          {
            Serial.println("Syntax error");
            lcd.print(" Syntax error ");
            resetCalc();
          }
         
    }
    else if(i == 2)
    {
          expr[1] = key-48;
          Serial.print(expr[1]);
      	  lcd.print(expr[1]);
      	  Serial.print(" = ");
      	  lcd.print(" = ");
          if(op == 'A')
          {
            Serial.println(expr[0] + expr[1]);
            lcd.print(expr[0] + expr[1]);
          }
          else if(op == 'B')
          {
            Serial.println(expr[0] - expr[1]);
            lcd.print(expr[0] - expr[1]);
          }
          else if(op == 'C')
          {
            Serial.println(expr[0] * expr[1]);
            lcd.print(expr[0] * expr[1]);
          }
          else if(op == 'D')
          {
            Serial.println(expr[0] / expr[1]);
            lcd.print(expr[0] / expr[1]);
          }

          i++;
    }
    else if(key == '*' || i == 3)
    {
      resetCalc();
    }
  }
}

Credits

anshul69

anshul69

0 projects • 0 followers

Comments