Anda di halaman 1dari 7

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'||ke
y=='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();
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(ans);
lcd.noAutoscroll();
}
else if (op == '-'){
ans = num1.toInt() - num2.toInt();
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(ans);
lcd.noAutoscroll();
}
else if (op == '*'){
ans = num1.toInt() * num2.toInt();
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(ans);
lcd.noAutoscroll();
}
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 = ' ';
}
}

/ Serial Summation

// This program can calculate the sum of a sequence of single digit

// numbers typed into the serial terminal.

byte myByte; // a variable to store a byte read from the serial

// buffer

long mySum; // a variable to hold the sum of the numbers read in from

// the serial buffer

int myNum; // a variable to hold the number being read in from the

// serial buffer

void setup() {

Serial.begin(9600); // begin serial communications

void loop() {

mySum = 0;

myNum = 0;
if (Serial.available()>0) {

while(Serial.available()>0){ // while bytes remain in the

//serial buffer

myByte = Serial.read(); // read in the current byte

// = is ASCII character 61

// 0-9 are ASCII characters 48 to 57

// - is ASCII character 45

// + is ASCII character 43

if (myByte == 61) { // equal sign found - could also do

/// if(myByte == '=')

Serial.print('='); delay(5); // need a short delay so the

// Serial.print doesn't garbble subsequent calculations

Serial.println(mySum); delay(5);

if (myByte >= 48 && myByte <= 57) { // found the first number

myNum = myByte-48;

mySum = myNum;

Serial.print(myNum); delay(5);

if (myByte == 45) { // found a minus sign

myByte = Serial.read(); // read in the next byte

myNum = myByte-48; // since ASCII number have codes of 48 to

// 57, this will convert to the decimal equivelent


Serial.print('-'); delay(5);

Serial.print(myNum); delay(5);

mySum = mySum - myNum; // subtract from total

if (myByte == 43) { // found a plus sign

myByte = Serial.read(); // read in the next byte

myNum = myByte-48;

Serial.print('+'); delay(5);

Serial.print(myNum); delay(5);;

mySum = mySum + myNum; // add to total

Anda mungkin juga menyukai