#include <LiquidCrystal.h>
#include <Keypad.h>
/* Display */
LiquidCrystal x(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte X_ROWS = 5;
const byte X_COLS = 4;
byte rowPins[X_ROWS] = {5, 4, 3, 2};
byte colPins[X_COLS] = {A3, A2, A1, A0};
char keys[X_ROWS][X_COLS] = {
{'1', '2', '3', '^'},
{'4', '5', '6', '%'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'},
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, X_ROWS, X_COLS);
uint64_t value = 0;
void showRandomScreen() {
x.print("Welcome");
x.setCursor(0, 1);
String message = "Processor";
for (byte i = 0; i < message.length(); i++) {
x.print(message[i]);
delay(50);
}
delay(500);
}
void updateRandom() {
if (millis() / 250 % 2 == 0 ) {
x.cursor();
} else {
x.noCursor();
}
}
void setup() {
Serial.begin(115200);
x.begin(16, 2);
showRandomScreen();
x.clear();
x.cursor();
x.setCursor(1, 0);
}
char randomOp = 0;
String store = "";
String randomVal = "";
uint64_t randomDecimal;
bool randomPoint = false;
double processRandom(char randomOp, double left, double right) {
switch (randomOp) {
case '^':
int exponent=right,result=1;
for (int i = 0; i < exponent; i++)
result *= left;
return result;
case '%':
while (left >= right) {
left -= right;
}
return left;
case '*':
return left * right;
case '/':
return left / right;
}
}
void randomInput(char key) {
if ('-' == key && randomVal == "") {
randomVal = "-";
x.print("-");
return;
}
switch (key) {
case '^':
case '%':
case '*':
case '/':
if (!randomOp) {
store = randomVal;
randomVal = "";
}
randomOp = key;
x.setCursor(0, 1);
x.print(key);
x.setCursor(randomVal.length() + 1, 1);
return;
case '=':
float leftNum = store.toDouble();
float rightNum = randomVal.toDouble();
store = String(processRandom(randomOp, leftNum, rightNum));
randomVal = "";
x.clear();
x.setCursor(1, 0);
x.print(store);
x.setCursor(0, 1);
x.print(randomOp);
return;
}
if ('.' == key && randomVal.indexOf('.') >= 0) {
return;
}
if ('.' != key && randomVal == "0") {
randomVal = String(key);
} else if (key) {
randomVal += String(key);
}
x.print(key);
}
void loop() {
updateRandom();
char key = keypad.getKey();
if (key) {
randomInput(key);
}
}