// libraries
#include <LiquidCrystal.h>
#include <Keypad.h>
// objects
LiquidCrystal lcd(0, 1, 2, 3, 4, 5); // Arduino pins
const byte ROWS = 4; //keypad rows number
const byte COLS = 4; //keypad column number.
//putting the keys you want for your keypad as array
char keys [ROWS] [COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '=', '/'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //Arduino connections for the rows pins
byte colPins[COLS] = {9, 8, 7, 6}; //Arduino connections for the columns pins
// Create keypad object
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// setup function
void setup()
{
lcd.begin(16,2); // the lcd's rows and columns
lcd.setCursor(0,0);
lcd.print("ITCE230Project");
lcd.setCursor(3,1);
lcd.print("Calculator");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("By:");
lcd.setCursor(5,0);
String massage = "Najim, ";
for (byte i = 0; i < massage.length(); i++) {
lcd.print(massage[i]);
delay(50);
}
lcd.setCursor(0,1);
String message1 = "Waleed,and Maged";
for (byte i = 0; i < message1.length(); i++) {
lcd.print(message1[i]);
delay(50);
}
delay(3000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Start ^_^");
delay(1000);
lcd.clear();
}
// for showing a blinking cursor in the lcd
// millis(): Returns the number of milliseconds passed since the Arduino board began running the current program
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
// define some basic variables for the calculator
char op = 0; // operation indicator
String previous; // the previous entered number (the first number)
String current; // the current entered number (the second number)
// calculate function
double calculate (char operation, double left, double right) {
switch (operation) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
}
}
// function for inputting data
void insertInput(char key) {
switch (key) {
case 'C': // clear case
op = 0;
current = "";
previous = "";
lcd.clear();
lcd.setCursor(0,0);
return;
case '+':
case '-':
case '*':
case '/':
if (!op) {
previous = current;
current = "";
}
op = key;
lcd.setCursor(0, 1);
lcd.print(key);
lcd.setCursor(current.length() + 1, 1); // so the numbers dont overwrite
return;
case '=':
double leftNumber = previous.toDouble(); // for enabling decimal number in division
double rightNumber = current.toDouble(); // for enabling decimal number in division
previous = String(calculate(op, leftNumber, rightNumber)); // saving the answer in "previous" using calculate function
current = ""; // clearing current""
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(previous); // displaying answer
lcd.setCursor(0, 1);
lcd.print(op);
return;
}
if (key) { // define current value
current += String(key);
}
lcd.print(key); // printing the value
}
// loop function
void loop() {
updateCursor();
char key = myKeypad.getKey(); // key getter method
if (key) {
insertInput(key); // use insertInput fundtion
}
}