#include <LiquidCrystal.h>
#include <Keypad.h>
// Set-up
char key;
const byte ROWS = 4;
const byte COLS = 4;
byte rowPins[ROWS] = {10, 9, 8, 7};
byte colPins[COLS] = {A3, A2, A1, A0};
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
const int RS = 12, E = 11, D4 = 5, D5 = 4, D6 = 3, D7 = 2;
LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
// Calculation
long first_num;
long second_num;
char operation;
bool non_number = false;
bool stop = false;
long first_num_int(long num1, char k) { // Function to "sum up" the input numbers.
String temp1 = String(num1);
temp1 += String(k - 48); // Convert the latest input number to a string.
// In ASCII, '1' is 49, '2' is 50 etc. Thats why there is a minus 48 in the code.
// This convert the number to a string for concatenation.
num1 = temp1.toInt(); // Convert the number to "long" after concatenation.
return num1;
}
long second_num_int(long num2, char k) {
String temp2 = String(num2);
temp2 += String(k - 48);
num2 = temp2.toInt();
return num2;
}
void loop() {
while (non_number == false) { // The loop will keep running until an operator key is pressed
key = keypad.waitForKey(); // Store the number pressed on the pad.
lcd.print(key); // Print out the key just pressed.
if (key == '.') { // Break the loop when '.' is pressed.
stop = true; // The program will not go into the loop below.
break;
}
if ((key == '+') || (key == '-') || (key == '*') || (key == '/')) { // An operator key is pressed.
non_number = true;
operation = key;
}
if (non_number == false) {
first_num = first_num_int(first_num, key); // Storing the numbers input and concatenate them.
}
}
non_number = false;
while ((non_number == false) && (stop == false)) { // The loop will keep running until the enter key is pressed
key = keypad.waitForKey();
if (key == '.') { // Break the loop when '.' is pressed.
stop = true;
break;
}
if (key != '=') {lcd.print(key);}
if (key == '=') { // When the equal sign is pressed.
non_number = true; // Stopping the loop
lcd.setCursor(0, 1); // To let the equal sign and the result print on the next row
lcd.print(key); // Print out the equal sign
}
if (non_number == false) { // Storing the numbers input and concatenate them.
second_num = second_num_int(second_num, key);
}
}
if (stop == false) { // Calculate the answer based on the first and second number stored.
// Calculation is done by considering different case (operator).
if (operation == '+') {lcd.print(first_num + second_num);}
if (operation == '-') {lcd.print(first_num - second_num);}
if (operation == '*') {lcd.print(first_num * second_num);}
if (operation == '/') {
if (second_num != 0) {lcd.print(first_num / second_num);} // General case of division where a number is not divided by zero.
if (second_num == 0) {lcd.print("ERROR");} // When the number is divided by zero.
}
}
if (stop == true) { // Program goes into this statement if '.' key (clear) is pressed at anytime.
lcd.clear(); // Erase everything in the screen.
stop = false;
}
// Restart calculation
first_num = 0; // Reset all
second_num = 0; // parameters
lcd.setCursor(0, 0); // to default
non_number = false; // values.
key = keypad.waitForKey();
lcd.clear();
if (key != '.') {lcd.print(key);}
if (non_number == false) {
first_num = first_num_int(first_num, key);
}
}