Arduino Calculator - Vathsav

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

8/31/2017 Arduino Calculator | Vathsav

Arduino Calculator
Saturday 15, November '14 Arduino (../categories/arduino)

This is a calculator that can perform basic arithmetic operations. This was done using an Arduino Uno, a
16x2 LCD display and a 4x4 dot matrix keypad.

The keypad library doesn't come with the Arduino IDE. You're gonna have to download
(http://playground.arduino.cc/Code/Keypad) and import it for you to be able to use the matrix keypad.
Place the downloaded file into the /libraries folder.

VIEW ON GITHUB (HTTPS://GITHUB.COM/VATHSAV/ARDUINO/BLOB/MASTER/CALCULATOR/CALCULATOR.INO) DOWNLOAD


SOURCE (../DOWNLOAD/ARDUINO_CALCULATOR.ZIP)

(../img/arduino_calculator/sketch.png)

This sketch was made using Fritzing (http://fritzing.org/), an awesome open source tool for bulding
prototypes.

http://www.vathsav.com/post/arduino_calculator.html 1/5
8/31/2017 Arduino Calculator | Vathsav

/*
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] [1] [-]
[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
*-------------------------------*

*/

The code is pretty simple. You're gonna have to define the keypad's layout in a 2 dimensional array, and
assign it to an instance of the Keypad class.

#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 );

In the setup function, we shall initialize the LCD and print a hello message for two and a half seconds.

//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.
}

http://www.vathsav.com/post/arduino_calculator.html 2/5
8/31/2017 Arduino Calculator | Vathsav

This loop function will listen for keypad inputs indefinitely.

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

HOME (HTTP://WWW.VATHSAV.COM) { BLOG } (HTTP://WWW.VATHSAV.COM/BLOG)


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 = ' ';
}
}

VIEW ON GITHUB (HTTPS://GITHUB.COM/VATHSAV/ARDUINO/BLOB/MASTER/CALCULATOR/CALCULATOR.INO) DOWNLOAD


SOURCE (../DOWNLOAD/ARDUINO_CALCULATOR.ZIP)

SHARE

Share Share

17 Comments Vathsav Harikrishnan 


1 Login

Sort by Best
 Recommend ⤤ Share

Join the discussion…


http://www.vathsav.com/post/arduino_calculator.html 3/5
8/31/2017 Arduino Calculator | Vathsav
Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Troller Hunter • 9 months ago


Why it keeps showing arrows and question marks ?
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > Troller Hunter • 9 months ago

Check your connections to the LCD. You see weird symbols if the jumpers are loosely connected.
△ ▽ • Reply • Share ›

Alejandro • a year ago


it does not work!
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > Alejandro • a year ago

Can you elaborate on what's wrong?


△ ▽ • Reply • Share ›

Kim • a year ago


Brilliant work! You are awesome!
△ ▽ • Reply • Share ›

Panagiotis Solomos • a year ago


Hi! What's the trimmer's type? How many ohms? 10k ohms? And the resistance's number is like the photo of the sketch? Thanks!
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > Panagiotis Solomos • a year ago

Hey!
Yes, I used a 10k trimmer and a 10k ohm resistor.
△ ▽ • Reply • Share ›

Leboyd Rodriguez • 2 years ago


Works great, although I used the analog inputs as connections to the rows of the keypad so can use one of the digital pins for other
purposes
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > Leboyd Rodriguez • 2 years ago

Cool! :)
△ ▽ • Reply • Share ›

This comment was deleted.

Vathsav Harikrishnan Moderator > Guest • 2 years ago

Hey Chinn,

You seem to have forgotten to include the LCD and Keypad libraries.

Add these two lines to the top of your code.

#include <liquidcrystal.h>
#include <keypad.h>
△ ▽ • Reply • Share ›

T Naveen Kumar • 2 years ago


really superb ..ra....vathsav..!!!
△ ▽ • Reply • Share ›

T Naveen Kumar • 2 years ago


hi...vathsav .... in starting keypad(in layout) one(number) is missing..!!
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > T Naveen Kumar • 2 years ago

Changed it now :) Thanks!


http://www.vathsav.com/post/arduino_calculator.html 4/5
8/31/2017 Arduino Calculator | Vathsav
△ ▽ • Reply • Share ›

िवलासराव • a year ago


1k resistor between to two positive supply not connected to any circuit is something missing here?
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > िवलासराव • a year ago

The pins on the breadboard are connected vertically. The right lead of the resistor is connected to 5V and the left lead is
connected to the LCD's backlight.
△ ▽ • Reply • Share ›

िवलासराव > Vathsav Harikrishnan • a year ago


Thanks for your reply. I'm not in confusion with blue wire but about the red one which is perpendicular to blue wire near
lcd
△ ▽ • Reply • Share ›

Vathsav Harikrishnan Moderator > िवलासराव • a year ago

You're right, it slipped my mind. There were 2 wires connected to the backlight. I've updated the sketch now.
Thanks man!
△ ▽ • Reply • Share ›

ALSO ON VATHSAV HARIKRISHNAN

Interfacing RFID with Arduino Android Toolbar


4 comments • 2 years ago• 2 comments • 2 years ago•

AvatarVathsav Harikrishnan — Sure :)The pin configuration on your RFID AvatarVathsav Harikrishnan — I'm glad it is :)
module might be different. Make sure you get the connections …

Real-time Chat Client Using Firebase Anxiety, Love, and Travel


4 comments • a year ago• 9 comments • 2 months ago•

AvatarPhil — Awesome!! AvatarVathsav Harikrishnan — I'm sorry for your loss, Lukasz. I cannot
imagine how it must have been. Things just don't make sense
sometimes and …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Privacy

ABOUT

Welcome to my blog! I am a programmer, designer, and a hardware and gaming enthusiast.


Learn more about me (../about).
Follow 167

SUBSCRIBE

Subscribe and receive updates for new posts!

Email Address

SUBSCRIBE

CATEGORIES

Android (../categories/android)
Arduino (../categories/arduino)
Particle Core (../categories/particle_core)
Processing (../categories/processing)
Rants (../categories/rants)

http://www.vathsav.com/post/arduino_calculator.html 5/5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy