Project showcase

Basic Calculator © GPL3+

Basic calculator using the arduino mega and without the keypad library

  • 259 views
  • 0 comments
  • 1 respect

Components and supplies

Necessary tools and machines

3drag
3D Printer (generic)
09507 01
Soldering iron (generic)
Hy gluegun
Hot glue gun (generic)

About this project

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 addition = 38;
int subtraction = 44;
int multiplication =48;
int division = 52;
int Clear = 7;
int equal = 40;


void readKey() {

  if (digitalRead(zero) == HIGH) {
    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;
  }
  else if (digitalRead(addition) == HIGH) {
    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(addition, INPUT);
pinMode(subtraction, INPUT);
pinMode(multiplication, INPUT);
pinMode(division, INPUT);
pinMode(Clear, INPUT);
pinMode(equal, INPUT);

  
lcd.begin(16,2);

}

void loop(){
readKey();
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;
  
}
}











 

Comments

Similar projects you might like

Pac-Man LED Pixel Panel Costume

Project tutorial by Ben Muller

  • 4,981 views
  • 4 comments
  • 86 respects

LoRa Gateway for DeviceHive

Project tutorial by DeviceHive IoT team

  • 1,320 views
  • 2 comments
  • 17 respects

IoT Bird Feeder with Sigfox and Tweeter

Project showcase by Gaël Porté

  • 404 views
  • 0 comments
  • 7 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 17,894 views
  • 6 comments
  • 45 respects

SmartWay

Project tutorial by Universum

  • 275 views
  • 0 comments
  • 5 respects

Arduino-Based Automatic Guitar Tuner

Project tutorial by Ben Overzat

  • 3,618 views
  • 0 comments
  • 12 respects
Add projectSign up / Login