Thursday 27 June 2013

Arduino Calculator

FINALLY! I'm done with my arduino calculator! Can now strike it off my bucket list. I sure did take my time to finish it. Started working on this shortly after my 12th board exams (28-03-2013). Had my college entrance exams in between, so I couldn't find time to work on it for more than 6 weeks. I needed jumper wires for connecting my keypad to the breadboard, which weren't available at any store nearby. Thanks to my cousin, Yogesh for linking me to simplelabs. Ordered it online and received it the very next day! (which is weird where I live :P).

Click To Enlarge

This schematic was created with Fritzing, an awesome open source application for prototyping and documenting. You can download it from here

You're gonna have to import the keypad library for you to be able to use the keypad. Extract it into your arudino directory /libraries folder, and you're good to go. You might as well download my .ino file from here.

This is a basic calculator. It is stable and I will continue to make this better and add more functionality as and when I find time. Feel free to share it and suggest improvements.


/*
Done by Vathsav Harikrishnan 
 Date Started [28-03-2013]
 Date Finished [27-06-2013]
 
 > Arduino Uno
 > 4x4 Dot Matrix Keypad
 > 16x2 LCD (With Backlight - Optional)
 > Breadboard
 > Connecting Wires, Female Jumper Wires
 > Resistors
 > Potentiometer




 
 Layout of 4x4 Dot Matrix Keyboard:
 ^
 C R >
 o o o o
 o o o o
 o o o o
 o o o o
 
 [7] [8] [9] [/]
 [4] [5] [6] [*]
 [3] [2] [2] [-]
 [X] [0] [=] [+]
 
 Press X to clear the screen
 
                      1 1 1 1 1 1
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
 *-------------------------------*
 |                            2 +|  <--- Memory here
 |                               |  <--- Cursor set here
 *-------------------------------*
 
 */

#include <LiquidCrystal.h> //import lcd library
#include <Keypad.h> //import keypad library

LiquidCrystal lcd(5, 4, 3, 2, 1, 0); //lcd pins
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns

//define the keymap
char keys [ROWS] [COLS] = {
  {'7', '4', '1', 'X'},
  {'8', '5', '2', '0'},
  {'9', '6', '3', '='},
  {'/', '*', '-', '+'}
};
byte rowPins[ROWS] = {
  9 ,8 ,7 ,6}; //connect keypad ROW1, ROW2, ROW3, ROW4 to these arduino pins
byte colPins[COLS] = {
  13, 12, 11, 10}; //connect keypad COL1, COL2, COL3, COL4 to these arduino pins

//create the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//variables declaration
boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1, num2;
int ans;
char op;

void setup(){
  lcd.begin(16,2);
  lcd.setCursor(2,0);
  lcd.print("Hello World!");
  delay(2500);
  lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner. 
}

void loop(){
  char key = myKeypad.getKey();

  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
    if (valOnePresent != true){
      num1 = num1 + key;
      int numLength = num1.length();
      lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
      lcd.print(num1);
    }
    else {
      num2 = num2 + key;
      int numLength = num2.length();
      lcd.setCursor(15 - numLength, 1);
      lcd.print(num2);
      final = true;
    }
  }

  else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){
    if (valOnePresent == false){
      valOnePresent = true;
      op = key;
      lcd.setCursor(15,0); //operator on right corner
      lcd.print(op);
    }
  }

  else if (final == true && key != NO_KEY && key == '='){
    if (op == '+'){
      ans = num1.toInt() + num2.toInt();
    }
    else if (op == '-'){
      ans = num1.toInt() - num2.toInt();
    }
    else if (op == '*'){
      ans = num1.toInt() * num2.toInt();
    }
    else if (op == '/'){
      ans = num1.toInt() / num2.toInt();
    }    
      lcd.clear();
      lcd.setCursor(15,0);
      lcd.autoscroll();
      lcd.print(ans);
      lcd.noAutoscroll();
  }
  else if (key != NO_KEY && key == 'X'){
    lcd.clear();
    valOnePresent = false;
    final = false;
    num1 = "";
    num2 = "";
    ans = 0;
    op = ' ';
  }
}

15 comments:

  1. Hi Vathsav this is a nice project however i am getting a number of compile errors when using this library for example NO_KEY is not defined in this scope ?

    ReplyDelete
    Replies
    1. Hey Steve, you need to place the Keypad library in your Arduino directory --> \Arduino\libraries\
      Download the Keypad library from http://playground.arduino.cc/Code/Keypad

      Delete
  2. Hey Vathsav,
    Where Can I download the LCD library from?

    ReplyDelete
    Replies
    1. Hey, you can download it from: http://playground.arduino.cc/Code/Keypad
      Make sure you place the LCD library in the Arduino\libraries\ directory.

      Delete
  3. I'm new to Aurduino.
    But i think the code bellow would reduce your code size a lot??

    if (op == '+'){
    ans = num1.toInt() + num2.toInt();
    }
    else if (op == '-'){
    ans = num1.toInt() - num2.toInt();
    }
    else if (op == '*'){
    ans = num1.toInt() * num2.toInt();
    }
    else if (op == '/'){
    ans = num1.toInt() / num2.toInt();
    }

    lcd.clear();
    lcd.setCursor(15,0);
    lcd.autoscroll();
    lcd.print(ans);
    lcd.noAutoscroll();

    ReplyDelete
    Replies
    1. Yes, It does reduce the size. I've changed it. :)

      Delete
  4. Hi I'm really new in arduino. is there any way to use 7 segments instead of lcd?

    ReplyDelete
  5. how many kilo the Resistors you have been used ?? thanks

    ReplyDelete
  6. I'm dumb at programming.. please give me some tips to excel in programming.... I'm new to arduino I have some great ideas but I'm not able to make programs for that.

    ReplyDelete
  7. how come the answer cant be more then 5 characters?

    ReplyDelete
    Replies
    1. The Arduino's int range is -32,768 to 32,767. You can use the 'long' datatype if you're working on large numbers. Long has a range -2,147,483,648 to 2,147,483,647.

      Delete
    2. do you know how you can make it divide into decimals?

      Delete
  8. Baccarat for Beginners - Worrione
    Baccarat is played by a small number of players. They can be played in various types of fixed games or on the move. Baccarat has a number of rules  1xbet korean Rating: worrione 5 หารายได้เสริม · ‎1 review

    ReplyDelete
  9. 888 Casino: $250 Welcome Bonus in November 2021
    888 Casino offers 보령 출장샵 a full suite of casino 군산 출장샵 games including slots, roulette, bingo 평택 출장안마 and 진주 출장안마 more. New players 문경 출장샵 get a $250 Welcome Bonus.

    ReplyDelete