Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
FZD1416
Published © GPL3+

Basic Calculator

Basic calculator using the arduino mega and without the keypad library

IntermediateShowcase (no instructions)6,122
Basic Calculator

Things used in this project

Hardware components

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

Hand tools and fabrication machines

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

Story

Read more

Code

Calculator_Code.ino

Arduino
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;
  
}
}











 

Credits

FZD1416
0 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.