//libraries for LCD screen
#include <LiquidCrystal.h>
#include <Keypad.h>
//LCD setup -- pins
const int rs=12, en=11, d4=5, d5=4, d6=3, d7=2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', 'x'},
{'*', '0', '=', '/'}
};
byte rowPins[ROWS] = {14, 15, 16, 17};
byte colPins[COLS] = {18, 19, 20, 21};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//Data Setups for inputs
//uses long to allow us to compute larger calculations as specified in manual
long value;
long* ptr = &value; //first number pressed by user
long value2;
long* ptr2 = &value2; //second number pressed by user
//pointer for operation entered, to store which operator is chosen
int state = 0;
int* ptr_state = &state;
//0 = first number being entered (neutral state - no operation declared yet)
//1 = addition
//2 = subtraction
//3 = multiplication
//4 = division
char nums[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
char operation[5] = {'+', '-', 'x', '/', '='};
void setup() {
Serial.begin(9600);
lcd.begin(16,2); //setup of lcd with rows, cols of screensize
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey == '*'){ //checking if clear screen is entered
lcd.clear();
*ptr = 0;
*ptr_state= 0; //resets back to 'entering first number' state
}
for (int i=0; i<10; i++){ //checking if a number is entered FIRST (in neutral state, before operation declared)
if ((customKey == nums[i]) && (*ptr_state == 0)){
int new_val = customKey - 48;
*ptr = *ptr * 10 + new_val; //multiplies original value by 10 so that when we add the new val, its shifted by one decimal
lcd.clear();
lcd.print(*ptr);
Serial.println("ptr:");
Serial.println(*ptr); //points to the entire entered number, & is updated with each new entry
//Serial.println(new_val);
}
}
//if something that is not a number is entered (an operation)
if (customKey == operation[0]){ //addition
*ptr_state = 1; //setting state to addition
lcd.print('+');
}
else if (customKey == operation[1]){ //subtraction
*ptr_state = 2;
lcd.print('-');
}
else if (customKey == operation[2]){ //multiplication
*ptr_state = 3;
lcd.print('x');
}
else if (customKey == operation[3]){ //division
*ptr_state = 4;
lcd.print('/');
}
else if (customKey == operation[4]) { //equal sign
lcd.setCursor(0,1); //setting cursor to bottom row
lcd.print("=");
if (*ptr_state == 1){ //add
lcd.print(*ptr + *ptr2);
}
else if (*ptr_state == 2){ //sub
lcd.print(*ptr - *ptr2);
}
else if (*ptr_state == 3){ //mult
lcd.print(*ptr * *ptr2);
}
else if (*ptr_state == 4){ //divide
if (*ptr2 == 0){
lcd.print("Error! Div by 0"); //error if division by zero
}
else;
lcd.print(*ptr / *ptr2);
}
*ptr_state = 0; //set state back to zero to input another first number
*ptr = 0;
*ptr2 = 0; //reset pointers
}
for (int i=0; i<10; i++){ //checking if a number is entered
if ((customKey == nums[i]) && (*ptr_state != 0)){ //checking if num added AFTER an operation is used
int new_val2 = customKey - 48;
*ptr2 = (*ptr2 * 10) + new_val2;
lcd.print(new_val2);
Serial.println("Ptr2:");
Serial.println(*ptr2);
}
}
delay(100);
}