Skip to content

Instantly share code, notes, and snippets.

@elktros
Created October 26, 2016 16:20
Show Gist options
  • Save elktros/5352febe09895d762e54c647f28bd238 to your computer and use it in GitHub Desktop.
Save elktros/5352febe09895d762e54c647f28bd238 to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
{'7', '8', '9', '/'},
{'4', '5', '6', '*'},
{'1', '2', '3', '-'},
{'C', '0', '=', '+'}
};
byte rowPins[ROWS] = {13 ,12 ,11 ,10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
int answer;
char op;
void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Electronics Hub ");
lcd.setCursor(0,1);
lcd.print(" Presents ");
delay(5000);
lcd.setCursor(0,0);
lcd.print(" Arduino based ");
lcd.setCursor(0,1);
lcd.print(" Calculator" );
delay(5000);
lcd.clear();
}
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 (presentValue != 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 (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
{
if (presentValue == false)
{
presentValue = true;
op = key;
lcd.setCursor(15,0);
lcd.print(op);
}
}
else if (final == true && key != NO_KEY && key == '='){
if (op == '+'){
answer = num1.toInt() + num2.toInt();
}
else if (op == '-'){
answer = num1.toInt() - num2.toInt();
}
else if (op == '*'){
answer = num1.toInt() * num2.toInt();
}
else if (op == '/'){
answer = num1.toInt() / num2.toInt();
}
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(answer);
lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'C'){
lcd.clear();
presentValue = false;
final = false;
num1 = "";
num2 = "";
answer = 0;
op = ' ';
}
}
@Royallinks
Copy link

Arduino: 1.8.7 (Windows 10), Board: "Arduino/Genuino Uno"

calculator_XX:2:20: error: Keypad.h: No such file or directory

compilation terminated.

The code was not uploaded cos of this "Keypad.h: No such file or directory"

@Royallinks
Copy link

exit status 1

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

@Obscure-glitch
Copy link

Use a library!

@Obscure-glitch
Copy link

Any commenting would be appreciated, good job on making coding for new-comers an absolute nightmare.

@Amirreza74Vafaei
Copy link

Hello, you just need to switch the code from :
{'7','8','9','D'},
{'4','5','6','C'},
{'1','2','3','B'},
{'*','0','#','A'}
to :

{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment