Project showcase

Basic calculator using the arduino mega and without the keypad library

• 4,864 views
• 13 respects

## Components and supplies

 Arduino Mega 2560
×1
 Adafruit Standard LCD - 16x2 White on Blue Any 16 X 2 LCD screens will work.
×1

## Necessary tools and machines

 3D Printer (generic)
 Soldering iron (generic)
 Hot glue gun (generic)

This is a school project where I decided to make a calculator. Most simple Arduino calculators online use a matrix keypad and the keypad library. I decided to give myself a challenge and stay away from the matrix keypad and keypad library.

## Code

##### Calculator_Code.inoArduino
Edit the pins where the lcd screen and buttons are connected. The code will work fine.
```#include <LiquidCrystal.h>

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

#define max_val 999999
float firstNum = 0;
float secondNum = 0;
float total = 0;
int insertNum;
int z = 1;
int y = 0;
long a;
int opAS = 0;
int NUM = 0;
int OP;

int one =30;
int two = 32;
int three = 42;
int four =28;
int five =26;
int six = 46;
int seven = 22;
int eight =24;
int nine =50;
int zero = 34;     //34
int decimal = 36;
int subtraction = 44;
int multiplication =48;
int division = 52;
int Clear = 7;
int equal = 40;

insertNum = 0;
}
else if (digitalRead(one) == HIGH) {
insertNum = 1;
}
else if (digitalRead(two) == HIGH) {
insertNum = 2;
}
else if (digitalRead(three) == HIGH) {
insertNum = 3;
}
else if (digitalRead(four) == HIGH) {
insertNum = 4;
}
else if (digitalRead(five) == HIGH) {
insertNum = 5;
}
else if (digitalRead(six) == HIGH) {
insertNum = 6;
}
else if (digitalRead(seven) == HIGH) {
insertNum = 7;
}
else if (digitalRead(eight) == HIGH) {
insertNum = 8;
}
else if (digitalRead(nine) == HIGH) {
insertNum = 9;
}
else if (digitalRead(decimal) == HIGH) {
insertNum = 10;
}
insertNum = 11;
}
else if (digitalRead(subtraction) == HIGH) {
insertNum = 12;
}
else if (digitalRead(multiplication) == HIGH) {
insertNum = 13;
}
else if (digitalRead(division) == HIGH) {
insertNum = 14;
}
else if (digitalRead(Clear) == HIGH) {
insertNum = 15;
}
else if (digitalRead(equal) == HIGH) {
insertNum = 16;
}
else{
insertNum = -1;
}

}

void setup()
{
pinMode(one, INPUT);
pinMode(two, INPUT);
pinMode(three, INPUT);
pinMode(four,INPUT);
pinMode(five, INPUT);
pinMode(six, INPUT);
pinMode(seven, INPUT);
pinMode(eight, INPUT);
pinMode(nine, INPUT);
pinMode(zero, INPUT);
pinMode(decimal, INPUT);
pinMode(subtraction, INPUT);
pinMode(multiplication, INPUT);
pinMode(division, INPUT);
pinMode(Clear, INPUT);
pinMode(equal, INPUT);

lcd.begin(16,2);

}

void loop(){
delay(200);

if(NUM == 0){
if (insertNum >= 0 && insertNum <= 9){
if(y==0){

a = pow(10, z);
firstNum = firstNum * (float)a + (float)insertNum;
}

if(y==1){
a = pow(10, z);
firstNum = (firstNum * (float)a + (float)insertNum)/(float)a;
z++;
}

if ( firstNum > max_val){
lcd.setCursor(0,0);
lcd.print("Num Too Large       ");
}

if ( firstNum <= max_val){
lcd.print("                   ");
lcd.setCursor(0,0);
lcd.print(firstNum);
}

}

if (insertNum == 10){
y++;

lcd.setCursor(0,1);
lcd.print("                   ");
lcd.setCursor(0,1);
lcd.print("       DECIMAL");

if(y == 2){
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print("error");
}
}
}

if(NUM == 1) {

if (insertNum >= 0 && insertNum <= 9){

if(y==0){

a = pow(10, z);
secondNum = secondNum * (float)a + (float)insertNum;
}

if(y==1){
a = pow(10, z);
secondNum = (secondNum * (float)a + (float)insertNum)/(float)a;
z++;
}

if ( secondNum > max_val){
lcd.setCursor(0,0);
lcd.print("Num Too Large       ");
}

if ( secondNum <= max_val){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(secondNum);
}

}

if (insertNum == 10){
y++;

lcd.setCursor(0,1);
lcd.print("                   ");
lcd.setCursor(0,1);
lcd.print("       DECIMAL");

if(y == 2){
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print("error");
}
}

}

if(insertNum == 11){
NUM = 1;
lcd.setCursor(0,1);
lcd.print("              +");

if( opAS >= 1){

if(OP == 1){
if( opAS == 1){
total =  (float) firstNum + (float)secondNum;
}
if (opAS >= 2){
total = (float) total + (float) secondNum;
}
}
if(OP == 2){
if( opAS == 1){
total =  (float) firstNum - (float)secondNum;
}
if (opAS >= 2){
total = (float) total - (float) secondNum;
}
}
if(OP == 3){
if( opAS == 1){
total =  (float) firstNum * (float)secondNum;
}
if (opAS >= 2){
total = (float) total * (float) secondNum;
}
}
if(OP == 4){
if( opAS == 1){
total =  (float) firstNum / (float)secondNum;
}
if (opAS >= 2){
total = (float) total / (float) secondNum;
}
}

lcd.setCursor(0,0);
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print(total);
}
opAS++;
secondNum = 0;
OP = 1;
y = 0;
z=1;
}

if(insertNum == 12){
NUM = 1;
lcd.setCursor(0,1);
lcd.print("              -");

if( opAS >= 1){

if(OP == 1){
if( opAS == 1){
total =  (float) firstNum + (float)secondNum;
}
if (opAS >= 2){
total = (float) total + (float) secondNum;
}
}
if(OP == 2){
if( opAS == 1){
total =  (float) firstNum - (float)secondNum;
}
if (opAS >= 2){
total = (float) total - (float) secondNum;
}
}
if(OP == 3){
if( opAS == 1){
total =  (float) firstNum * (float)secondNum;
}
if (opAS >= 2){
total = (float) total * (float) secondNum;
}
}
if(OP == 4){
if( opAS == 1){
total =  (float) firstNum / (float)secondNum;
}
if (opAS >= 2){
total = (float) total / (float) secondNum;
}
}

lcd.setCursor(0,0);
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print(total);
}

opAS++;
secondNum = 0;
OP=2;
y = 0;
z = 1;
}

/*
if(insertNum == 13){
NUM = 1;
lcd.setCursor(0,1);
lcd.print("              *");

if( opAS >= 1){

if(OP == 1){
if( opAS == 1){
total =  (float) firstNum + (float)secondNum;
}
if (opAS >= 2){
total = (float) total + (float) secondNum;
}
}
if(OP == 2){
if( opAS == 1){
total =  (float) firstNum - (float)secondNum;
}
if (opAS >= 2){
total = (float) total - (float) secondNum;
}
}
if(OP == 3){
if( opAS == 1){
total =  (float) firstNum * (float)secondNum;
}
if (opAS >= 2){
total = (float) total * (float) secondNum;
}
}
if(OP == 4){
if( opAS == 1){
total =  (float) firstNum / (float)secondNum;
}
if (opAS >= 2){
total = (float) total / (float) secondNum;
}
}

lcd.setCursor(0,0);
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print(total);
}

opAS++;

OP=3;
secondNum = 0;
}
*/

if(insertNum == 13){
NUM = 1;
lcd.setCursor(0,1);
lcd.print("              *");

if( opAS > 0){

if(OP == 1){
if( opAS == 1){
total =  (float) firstNum + (float)secondNum;
}
if (opAS >= 2){
total = (float) total + (float) secondNum;
}
}
if(OP == 2){
if( opAS == 1){
total =  (float) firstNum - (float)secondNum;
}
if (opAS >= 2){
total = (float) total - (float) secondNum;
}
}
if(OP == 3){
if( opAS == 1){
total =  (float) firstNum * (float)secondNum;
}
if (opAS >= 2){
total = (float) total * (float) secondNum;
}
}
if(OP == 4){
if( opAS == 1){
total =  (float) firstNum / (float)secondNum;
}
if (opAS >= 2){
total = (float) total / (float) secondNum;
}
}

lcd.setCursor(0,0);
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print(total);
}

opAS++;
secondNum = 0;
OP=3;
y=0;
z=1;
}

if(insertNum == 14){
NUM = 1;
lcd.setCursor(0,1);
lcd.print("              /");

if( opAS >= 1){

if(OP == 1){
if( opAS == 1){
total =  (float) firstNum + (float)secondNum;
}
if (opAS >= 2){
total = (float) total + (float) secondNum;
}
}
if(OP == 2){
if( opAS == 1){
total =  (float) firstNum - (float)secondNum;
}
if (opAS >= 2){
total = (float) total - (float) secondNum;
}
}
if(OP == 3){
if( opAS == 1){
total =  (float) firstNum * (float)secondNum;
}
if (opAS >= 2){
total = (float) total * (float) secondNum;
}
}
if(OP == 4){
if( opAS == 1){
total =  (float) firstNum / (float)secondNum;
}
if (opAS >= 2){
total = (float) total / (float) secondNum;
}
}

lcd.setCursor(0,0);
lcd.print("                  ");
lcd.setCursor(0,0);
lcd.print(total);
}

opAS++;
secondNum = 0;
OP=4;
y=0;
z=1;
}

if(insertNum == 16){
if(NUM == 1){
if(OP == 1){
if( opAS == 1){
total =  (float) firstNum + (float)secondNum;
}
if (opAS >= 2){
total = (float) total + (float) secondNum;
}
}
if(OP == 2){
if( opAS == 1){
total =  (float) firstNum - (float)secondNum;
}
if (opAS >= 2){
total = (float) total - (float) secondNum;
}
}
if(OP == 3){
if( opAS == 1){
total =  (float) firstNum * (float)secondNum;
}
if (opAS >= 2){
total = (float) total * (float) secondNum;
}
}
if(OP == 4){
if( opAS == 1){
total =  (float) firstNum / (float)secondNum;
}
if (opAS >= 2){
total = (float) total / (float) secondNum;
}
}

lcd.clear();
lcd.setCursor(0,0);
lcd.print("=");
lcd.setCursor(1,0);
lcd.print(total);
}
if(NUM == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(firstNum);
}

}

if(insertNum == 15){
lcd.clear();
total = 0;

z = 1;
y = 0;
firstNum = 0;
secondNum = 0;
opAS = 0;
NUM = 0;

}
}

```

• 1 project
• 2 followers

November 5, 2017

#### Members who respect this project

and 6 others

See similar projects
you might like

#### Simple UNO calculator

Project tutorial by Joprp05

• 31,888 views
• 26 respects

#### Basic Ultrasonic Tutorial

• 14,289 views
• 44 respects

Project tutorial by evive: an opensource embedded platoform

• 11,169 views
• 18 respects

#### 'Roger Bot' the Pet Rover

Project showcase by hannu_hell

• 5,628 views
• 15 respects

#### Candy Dispenser with Google Assistant

Project tutorial by Arduino “having11” Guy

• 22,054 views