/*
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 = 1;
const byte ROW = 2;
const byte COL = 2;
String memory = "";
String memory2 = "";
String memory3 ="";
String memory4;
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','=','/'}};
char keys2[ROW][COL] = {
{'M','N'},
{'R','C'}
};
// connect keypad with columns and rows with arduino
byte rowPins[ROWS] = { 15, 16, 17, 18 };
byte colPins[COLUMNS] = {A3, A2, A1, A0 };
byte RP[ROW] = { A8, A9};
byte CP[COL] = {A13, A14};
//makinng keypad
Keypad K = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLUMNS);
Keypad K2 = Keypad(makeKeymap(keys2),RP,CP,ROW,COL);
// 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 Password() {
lcd.print("Type ");
lcd.setCursor(3, 1);
String message = "Calculator";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}
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();
}
double CalculateResult(char process, double number1, double number2)
{
switch (process) { // using switch instead of if-else to shorten
case '+': return number1 + number2; // operation will proceed according to the pressed button
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 = "";
memory3 = memory; ////////////////////
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(memory);
lcd.setCursor(0, 1);
lcd.print(process);
return;
/*
case 'M':
// memory3 = memory + memory3;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(memory3);
Serial.println("M+");
// return;
case 'N':
// memory3 = memory3;
lcd.clear();
lcd.setCursor(1, 0);
// lcd.print(memory3);
Serial.println("M-");
// return;
case 'R':
// memory3 = "";
lcd.clear();
lcd.setCursor(1, 0);
// lcd.print(memory3);
Serial.println("Mr");
// return;
case 'C':
// memory3 = "";
lcd.clear();
lcd.setCursor(1, 0);
// lcd.print(memory3);
Serial.println("Mc");
// 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() { // Not implimented, should interept and restart the program
wdt_disable();
wdt_enable(WDTO_15MS);
while (1) {}
}
void DetectButtons()
{
int buttonState = digitalRead(BUTTON_PIN);
if(buttonState == LOW){
lcd.noDisplay();
lcd.noCursor();
}
if(buttonState == HIGH){
lcd.display();
lcd.cursor();
}
}
void mem(char key2){
if (key2 =
switch(key2){
case 'M':
// memory4 = memory3;
lcd.clear();
lcd.setCursor(1, 0);
// lcd.print(memory4);
}
}
void loop() {
DetectButtons();
char key = K.getKey();
char key2 = K2.getKey();
if (key) {
operation(key);
}
if (key2){
mem(key2);
}
}