Introduction: LCD Calculator by Jai Mishra

This is a very useful project that teaches you how to create your own calculator. You can either create this calculator online or in real life with the help of extra supplies but for now we are only going to focus on creating an online calculator.

Supplies

  • Arduino Uno R3
  • 220 Ohms resistor
  • 4*4 Keypad
  • 16*2 LCD
  • Bunch of wires to connect the circuit

Step 1: Gather Your Supplies on TinkerCad

Make sure all of your supplies are available before we begin with step 2 to reduce stress and mistakes. Make sure to also use the right supplies, some of the components on this image above are similar to other components, so do not get mixed in between. Use the image above as your guidance.

Step 2: Arrange Your Supplies

Arranging your supplies is the best way to see a preview of what your calculator might look like. You can create any type of calculator design you want but make sure the calculator looks natural and users can understand the design and not get confused. I used a typical classy calculator design which is effective and understandable by everyone. You can either choose my design or create your own, but whatever it is be creative and good luck!

Step 3: Connecting the Wires

Connecting the wires is a hard job if you do not understand the meaning behind it. In this wiring, we are trying to connect all four components together so they can work as a group when it is time for writing the code. If there are no wires, no current is going to flow, leading to a failed project. Make sure your wires are connected properly without any misunderstanding.

After you are done connecting the wires, make sure your wires are neat and organized so it is easier for you and others to understand what exactly is going on in the hardware of this calculator. Like I said before, you can either use my technique of organizing your wires or you can create your own, but whatever you decide to do, make sure they are assembled with some space.

Step 4: Writing the Code

#include

#include #include

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

long first = 0; long second = 0; double total = 0;

char customKey; const byte ROWS = 4; const byte COLS = 4;

char keys[ROWS][COLS] = { {'1','4','7','/'}, {'2','5','8','+'}, {'3','6','9','-'}, {'C','0','=','*'} }; byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() { lcd.begin(16, 2); // start lcd for(int i=0;i<=3;i++); lcd.setCursor(0,0); lcd.print("Calculator"); lcd.setCursor(0,1); lcd.print("By Jai Mishra"); delay(4000); lcd.clear(); lcd.print("Final Project"); delay(2500); lcd.clear(); lcd.setCursor(0, 0); }

void loop() {

customKey = customKeypad.getKey(); switch(customKey) { case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/" lcd.setCursor(0,0); first = first * 10 + (customKey - '0'); lcd.print(first); break;

case '+': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("+"); second = SecondNumber(); // get the collected the second number total = first + second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; // reset values back to zero for next use break;

case '-': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("-"); second = SecondNumber(); total = first - second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break;

case '*': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("*"); second = SecondNumber(); total = first * second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break;

case '/': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("/"); second = SecondNumber(); lcd.setCursor(0,3);

second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;

lcd.print(total); first = 0, second = 0; break;

case 'C': total = 0; lcd.clear(); break; } }

long SecondNumber() { while( 1 ) { customKey = customKeypad.getKey(); if(customKey >= '0' && customKey <= '9') { second = second * 10 + (customKey - '0'); lcd.setCursor(0,2); lcd.print(second); }

if(customKey == '=') break; //return second; } return second; }

Step 5: Breaking Down the Code

We initialized the values for the computer to understand

#include

#include #include

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

long first = 0; long second = 0; double total = 0;

char customKey; const byte ROWS = 4; const byte COLS = 4;

We told the computer the numbers and signs of which the Keypad should work to.

char keys[ROWS][COLS] = {
{'1','2','3','/'}, {'4','5','6','+'}, {'7','8','9','-'}, {'C','0','=','*'} };

We finalized the rows and columns of the keypad and which number comes in which column, etc.

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

We created the intro, or the power on screen for the computer(You can write your own name on it).

void setup()
{ lcd.begin(16, 2); for(int i=0;i<=3;i++); lcd.setCursor(0,0); lcd.print("Calculator"); lcd.setCursor(0,1); lcd.print("By Jai Mishra"); delay(4000); lcd.clear(); lcd.print("Final Project"); delay(2500); lcd.clear(); lcd.setCursor(0, 0); }

We create the meaning and the formula for each operation in the calculator so the computer understands what formula to use when the user presses "+" on the calculator, etc.

{
case '0' ... '9': lcd.setCursor(0,0); first = first * 10 + (customKey - '0'); lcd.print(first); break;

case '/': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("/"); second = SecondNumber(); lcd.setCursor(0,3);

second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;

lcd.print(total); first = 0, second = 0; break; case '+': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("+"); second = SecondNumber(); lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break;

case '-': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("-"); second = SecondNumber(); total = first - second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break;

case '*': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print("*"); second = SecondNumber(); total = first * second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break;

case 'C': total = 0; lcd.clear(); break; } }

The code is very easy, all you have to do is try to understand it and then everything can be done easily. If any problems with the code, email me.

Step 6: How Does the Hardware of This Calculator Work?

This calculator uses, an LCD, a keypad, an Arduino board and a 220 ohms resistor. All of these components are separate but are connected with the wires from the Arduino to the keypad and the LCD. Different sections of the LCD are connected with the Arduino board which ultimately connects both of them with Keypad. After the connection, the coding does all of the work and gives each operation and button on the keypad a job to follow.

Step 7: Full Preview of the Calculator

This is what our final project looks like! If your code does not work, or there are some technical difficulties then please email me and I will try my best to help you create the best calculator!

Step 8: My Inspiration of This Code!

I got inspired from the video above on how to make a calculator on tinkercad! I did not copy and paste anything but I did use his idea of the calculator and the understanding of the code.