Anda di halaman 1dari 7

Experiment-2

AimMake a simple calculator using matrix keypad and LCD display. Extend this calculator to
Implement the SQRT() function. Finally display answer in Hindi font also.

ApparatusArduino, 4x4 Matrix Keypad, 16x2 LCD, potentiometer, Breadboard, Wire Cutter, Connecting
Wires, Male-Female Jumper Wires, Resistors.

Challenges Faced

There is limit of calculation in this calculator, because of it we cant get answer greater than
2^15 or 32768.
The simple calculator (+, -, x, /) needs two inputs but in square root operation, we can only give
one input.
The matrix keypad have only 16 keys, so for sqrt operation we have to replace one key
operation.
In Hindi fonts of numbers, we have to make extra loop to get the correct response when the
output greater than one digit.

Circuit Diagram-

Figure 1. (a) Circuit Diagram for Arduino Matrix Calculator [1] (b) Our Circuit

Flow Chart-

Start

Input a

Operation

NO

Input b

sqrt()
YES

NO

Ans=sqrt(a)

NO

NO

YES

YES
Ans=a-b

Ans=a+b

YES
Ans=axb

YES
b=0
NO
Ans=a/b

Not Defined

Ans

Stop

ObservationsSome Sample calculations of our calculator(a) Add operationInput =12+42


Output =56
(b) Subtract operationInput = 234-125
Output=109
(c) Square Root Operation
Input= 16? (Here we use ? sign for square root operation)
Output =4
(d) Divide operation
Input =1/0
Output=Absurd
(e) Multiplication
Input = 24x11
Output= 264 (In Hindi Numbers also)

Figure 2. (a) Input =12+42 (b) Output=54

Figure 3. (a) Input =1/0 (b) Output=absurd

Figure 4. (a) Input =24*11 (b) Output=264

Code for CalculatorSource [2]


/*
> 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:
^
CR>
oooo
oooo
oooo
oooo
[1] [2] [3] [+]
[4] [5] [6] [- or ?]
[7] [8] [9] [*]
[X] [0] [=] [/]
Press X to clear the screen
*/
#include <LiquidCrystal.h> //import lcd library
#include <Keypad.h> //import keypad library
#include <math.h>
LiquidCrystal lcd(5, 4, 3, 2, 1, 0); //lcd pins
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
byte One[8] = {
0b00100, 0b01010, 0b01110, 0b00010, 0b00100, 0b01000, 0b00100, 0b00010
};
byte Two[8] = {
0b00100, 0b01010, 0b00010, 0b00010, 0b01100, 0b00100, 0b00010, 0b00001
};
/*byte Three[8] = {
0b11110, 0b00010, 0b00010, 0b11110, 0b00010, 0b11010, 0b11110, 0b01000
};*/
byte Four[8] = {
0b10001, 0b10001, 0b01010, 0b00100, 0b01010, 0b01010, 0b01110, 0b00000
};
byte Five[8] = {
0b10000, 0b10000, 0b10110, 0b10110, 0b11110, 0b00010, 0b00010, 0b00010
};
byte Six[8] = {
0b11110, 0b10000, 0b10000, 0b11110, 0b10000, 0b10110, 0b11110, 0b00100
};
byte Seven[8] = {
0b00000, 0b00000, 0b00000, 0b10111, 0b10101, 0b10111, 0b10001, 0b01110
};
byte Eight[8] = {
0b00010, 0b00100, 0b01000, 0b10000, 0b10001, 0b10001, 0b10001, 0b01110
};
byte Nine[8] = {
0b01110, 0b01010, 0b01010, 0b01100, 0b00100, 0b00010, 0b00001, 0b00011
};
//define the keymap
char keys [ROWS] [COLS] = {
{'/', '=', '0', 'X'},

{'*', '9', '8', '7'},


{'?', '6', '5', '4'},
{'+', '3', '2', '1'}
};
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, absurd ;
int ans;
char op;
void setup(){
lcd.begin(16,2);
lcd.setCursor(2,0);
lcd.print("Hello World!");
delay(2500);
lcd.clear();
lcd.createChar(1,One);
lcd.createChar(2,Two);
lcd.createChar(4,Four);
lcd.createChar(5,Five);
lcd.createChar(6,Six);
lcd.createChar(7,Seven);
lcd.createChar(8,Eight);
lcd.createChar(9,Nine);//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;
if(op=='?'){
final = true;
}
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 = sqrt(num1.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();
if (op == '/' && num2.toInt() == 0 ) {
lcd.print ("Absurd");
}
else {
lcd.print(ans);
int nnem;
int i=15;
while(ans!=0){
lcd.setCursor(i,1);
nnem=ans%10;
ans=ans/10;
lcd.write(nnem);
i=i-1;
}
}
lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'X'){
lcd.clear();
valOnePresent = false;
final = false;
num1 = "";
num2 = "";
ans = 0;
op = ' ';
}
}

Results and Conclusions1. We can implement calculator operations using Arduino and Matrix Keypad.
2. There is a limit for calculations in this Calculator. We cant get the result more than 2^15.
3. We can only give eight characters for Hindi number fonts, so we neglect 3 and 0 because they
are same in both fonts.

References[1] http://www.vathsav.com/post/arduino_calculator.html
[2] https://github.com/vathsav/Arduino/blob/master/calculator/calculator.ino
[3] https://omerk.github.io/lcdchargen/
[4] https://www.arduino.cc/en/Reference/LiquidCrystal

Anda mungkin juga menyukai