Arduino Calculator

Arduino Calculator

Check out the tutorial here:

#include [LiquidCrystal.h]
#include [Keypad.h]
const byte rows=4;
const byte columns=4;
char keymap [rows] [columns]= {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowpins [rows]={2,3,4,5};
byte columnpins [columns]={6,7,8,9};
Keypad kibrdkeys= Keypad( makeKeymap(keymap), rowpins, columnpins, rows, columns);
const int rs=A0, en=A1, d4=A2, d5=A3, d6=A4, d7=A5;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
String n,n1,n2;
char operation, key;
boolean terminate= false;
int result;
void setup() {
// put your setup code here, to run once:
lcd.begin(16,2);
lcd.print("wlcm");
delay(2000);
lcd.clear();
}
void loop() {
displayresult();
key=kibrdkeys.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') {
n=n+key;
lcd.setCursor(0,1);
lcd.print(n);
}
if (key!=NO_KEY && key=='B' || key=='C' || key=='*' || key=='#') {
lcd.clear();
n1=n;
n="" ;
if (key=='B')
operation='+';
if (key=='C')
operation='-';
if (key=='*')
operation='*';
if (key=='#')
operation='/';
}
if (key!=NO_KEY && key=='D') {
lcd.clear();
n2=n;
terminate=true;
if (operation=='+') {
result= n1.toInt()+n2.toInt();
}
if (operation=='-') {
result= n1.toInt()-n2.toInt();
}
if (operation=='*') {
result= n1.toInt()*n2.toInt();
}
if (operation=='/') {
result= n1.toInt()/n2.toInt();
}
}
if (key!=NO_KEY && key=='A'){
lcd.clear();
n1="0";
n2="0";
result=0;
n="";
terminate=false;
}
}
void displayresult() {
lcd.setCursor(0,0); lcd.print(n1);lcd.print(operation); lcd.print(n2);
if (terminate==true) {
lcd.print("="); lcd.print(result);}
}
NOTE: Please change [] this sign to <> in first two lines.

Comments