#include <LiquidCrystal.h>
#include <Keypad.h>
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4; // Represent number of rows and columns
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2}; // Makes an array
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0}; //
//rows and columns are switched. (j,i) format.
//so below, (0,3) is 3rd row, 0th column.
char keys[KEYPAD_ROWS][KEYPAD_COLS] = { // 2D array showing layout of keypad
{'1', '2', '3', '+'}, // Each element here is a button on the keypad.
{'4', '5', '6', '-'}, // Ex, '+' is first row, 4th column of keypad = keys[0][3] which is A.
{'7', '8', '9', '*'},
{'C', '0', '=', '/'} // Inspired by Wokwi electronic safe design
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
// Creates a Keypad object named keypad for use later.
// https://forum.arduino.cc/t/keypad-libary-question/553761
long first = 0; //want longs to store lots of decimals for sig digs.
long second = 0;
long total = 0;
char key;
char index = 1;
bool resultsflag = false;
void setup() { // Inspired by Wokwo electronic safe
lcd.begin(16, 2); //starts.
lcd.print("Calculator");
lcd.setCursor(0, 0);
delay(2000); // Waits for 1000ms = 1 second
lcd.clear(); // Clears after the 1 second.
}
void loop() {
key = keypad.getKey(); // Reads key pressed.
switch(key)
{
case 'C': //want to have a clear case.
total = 0;
first = 0;
second = 0;
index = 1;
lcd.clear();
break;
case '0' ... '9': //had to google the syntax to use. case 'thing' then colon.
lcd.setCursor(0,0); // start in top left position.
first = first * 10 + (key - '0'); //should allow for adding multiple concatenations.
if (resultsflag) {
lcd.clear();
resultsflag = false;
}
lcd.print(first);
index += 1;
break;
case '+': //addition
if (resultsflag) {
lcd.clear();
total = 0;
first = 0;
second = 0;
index = 1;
lcd.clear();
resultsflag = false;
}
if (total != 0) {
first = total; }
else {
first = first; }
//lcd.setCursor(0,0);
lcd.print("+"); //want to display the plus sign.
second = SecondNumber();
total = first + second; //does math.
lcd.setCursor(0,3); //lowers cursor to next line.
lcd.print(total);
resultsflag = true;
first = 0, second = 0; //clears values.
break; //end case.
case '-': // subtraction
if (resultsflag) {
lcd.clear();
total = 0;
first = 0;
second = 0;
index = 1;
lcd.clear();
resultsflag = false;
}
if (total != 0) {
first = total; }
else {
first = first; }
//lcd.setCursor(1,0);
lcd.print("-"); //want to display the - sign.
second = SecondNumber();
total = first - second; //does math. order of ops matters. first - second.
lcd.setCursor(0,3); //lowers cursor to next line.
lcd.print(total);
resultsflag = true;
first = 0, second = 0; //resets values.
break; //end case.
case '*': //multiplication
if (resultsflag) {
lcd.clear();
total = 0;
first = 0;
second = 0;
index = 1;
lcd.clear();
resultsflag = false;
}
if (total != 0) {
first = total; }
else {
first = first; }
//lcd.setCursor(1,0);
lcd.print("*"); //display the * sign.
second = SecondNumber();
total = first * second; //does multiplication
lcd.setCursor(0,3); //lowers cursor to next line.
lcd.print(total);
resultsflag = true;
first = 0, second = 0; //resets values.
break; //end case.
case '/': //need to have special case for division by 0.
if (resultsflag) {
total = 0;
first = 0;
second = 0;
index = 1;
lcd.clear();
resultsflag = false;
}
if (total != 0) {
first = total; }
else {
first = first; }
//lcd.setCursor(1,0);
lcd.print("/"); //display the / sign.
second = SecondNumber();
lcd.setCursor(0,3); //lowers cursor to next line.
if (second == 0) { //this will test for dividing by 0.
lcd.print("Invalid: Division by 0");
resultsflag = true; }
else {
total = first / second;
}
lcd.print(total);
resultsflag = true;
first = 0, second = 0; //resets values.
break; //end case.
}}
long SecondNumber()
{
while (1) //while true
{
key = keypad.getKey(); //get input
if (key >= '0' && key <= '9') //if a number. all other cases covered above.
{
second = second * 10 + (key - '0');
lcd.setCursor(index,0); //puts 2nd number on top line, 3rd spot. so first number, room for 2nd.
lcd.print(second); //print to screen.
//so at this point, first is on line 1, second line 2, and operation line 3.
}
if (key == 'C'){
total = 0;
first = 0;
second = 0;
index = 1;
lcd.clear();
break;
}
if (key == '=')
break; //stop when equals sign is pressed.
}
return second;
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//first attempt.
// if (key != NO_KEY) { // For some reason you have to include this. Basically an if statement that says if a key is pressed.
// // First step - get numbers. Want to make sure if multidigit, will concatenate into string.
// if (key >= '0' && key <= '9') { // Getting digit between 0-9.
// input += key; // Add digit to string. Accounts for multidigit numbers.
// lcd.print(key); // Want to print it out.
// }
// else if (key == '+' || key == '-' || key == '/' || key == '*') {
// num1 = input.toInt();
// operation = key; // Store the operation in a new variable.
// lcd.clear(); // Clear the display before printing the operation
// lcd.print(operation); // Display the operation sign
// } else if (key == '=') {
// num2 = input.toInt();
// calculate(); // Perform calculation. Made a calculate function.
// } else if (key == '.') {
// input = ""; // Clear the input string
// lcd.clear(); // Clear it.
// }
// }
// }
// void calculate() {
// int result; // Make new variable called result.
// if (operation == '+') {
// result = num1 + num2;
// } else if (operation == '-') {
// result = num1 - num2;
// } else if (operation == '*') {
// result = num1 * num2;
// } else if (operation == '/') {
// if (num2 == 0) {
// lcd.print("Error: Divide by 0"); // Display error message
// delay(2000); // Wait for 2 seconds
// lcd.clear(); // Clear the display
// return;
// } else {
// result = num1 / num2;
// }
// }
// lcd.print(result); // Display the result on LCD
// }