/*
References
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
https://youtu.be/CNiWWX15Zns
https://docs.wokwi.com/parts/wokwi-lcd1602
https://docs.wokwi.com/parts/wokwi-membrane-keypad
https://create.arduino.cc/projecthub/123samridhgarg/arduino-calculator-bb57c7
https://docs.wokwi.com/parts/wokwi-pushbutton
https://wokwi.com/arduino/libraries/demo/simon-game
https://wokwi.com/projects/288681423014986248
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <avr/wdt.h>
#include <EEPROM.h>
const byte ROWS = 4;
const byte COLUMNS = 4;
const byte BUTTON_PIN = 19;
String memory = "";
String memory2 = "";
char process = 0;
int lastState = HIGH;
// mapping the keys
//source https://docs.wokwi.com/parts/wokwi-membrane-keypad
char keys[ROWS][COLUMNS] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'.','0','=','/'}};
// connect keypad with columns and rows with arduino
byte rowPins[ROWS] = { 15, 16, 17, 18 };
byte colPins[COLUMNS] = {A3, A2, A1, A0 };
//makinng keypad
Keypad K = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLUMNS);
// Assign LCD with the pins
const int rs=12;
const int en=11;
const int d4=10;
const int d5=9;
const int d6=8;
const int d7=7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
long Num1,Num2,Number;
char key,action;
boolean result = false;
void setup()
{
lcd.clear();
//Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
//digitalWrite(BUTTON_PIN,LOW);
lcd.begin(16, 2);
lcd.setCursor(0, 1);
lcd.clear();
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), DetectButtons, HIGH);
}
double CalculateResult(char process, double number1, double number2)
{
switch (process) { // using switch instead of if-else to shorten
case '+': return number1 + number2;
case '-': return number1 - number2;
case '*': return number1 * number2;
case '/': return number1 / number2;
}
}
void operation(char key) {
if ('-' == key && memory2 == "") {
memory2 = "-";
lcd.print("-");
return;
}
switch (key) {
case '+':
case '-':
case '*':
case '/':
if (!process) {
memory = memory2;
memory2 = "";
}
process = key;
lcd.setCursor(0, 1);
lcd.print(key);
lcd.setCursor(memory2.length() + 1, 1);
return;
case '=':
float Number1 = memory.toDouble(); //converting to floting point
float Number2 = memory2.toDouble();
memory = String(CalculateResult(process, Number1, Number2));
memory2 = "";
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(memory);
lcd.setCursor(0, 1);
lcd.print(process);
return;
}
if ('.' == key && memory2.indexOf('.') >= 0)
{
return;
}
if ('.' != key && memory2 == "0")
{
memory2 = String(key);
}
else if (key)
{
memory2 += String(key);
}
lcd.print(key);
}
void CursorBlink() {
if (millis() / 250 % 2 == 0 ) { //curser blinks every 1/4th seconds or 250 mili seconds
lcd.cursor();
} else {
lcd.noCursor();
}
}
void reboot() {
wdt_disable();
wdt_enable(WDTO_15MS);
while (1) {}
}
void DetectButtons()
{
/*
int value = digitalRead((BUTTON_PIN));
if (lastState != value) {
lastState = value;
if (value == HIGH)
{
num = "";
num2= "";
Serial.println(" released");
}
} */
memory = "";
memory2 = "";
Num1 = 0;
Num2 = 0;
Number = 0;
key = 0;
action = 0;
lcd.clear();
//Serial.end();
}
/*
void loop()
{
CursorBlink();
key =K.getKey();
if (key!=NO_KEY)
DetectButtons();
if (key)
{
result = true;
// CalculateResult();
operation(key);
}
}
void setWDT ()
{
WDTCSR |= 0b00011000;
WDTCSR = sWDT | WDTO_1S;
wdt_reset();
}
ISR (WDT_vect)
{
digitalWrite(led, HIGH);
if(wSetting == 3) {
byte b = 1;
EEPROM.put(10, b);
}
//wdt_disable();
}
*/
void loop() {
CursorBlink();
char key = K.getKey();
if (key) {
operation(key);
}
}