//libraries
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Display
// set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27,20,4);
// Keypad setup
const uint8_t ROWS = 5;
const uint8_t COLS = 5;
char keys[ROWS][COLS] = {
{'F', 'C', '√', '%', '÷'},
{'A', '7', '8', '9', '*'},
{'B', '4', '5', '6', '-'},
{'X', '1', '2', '3', '+'},
{'Y', '0', '.', 'P', '='}
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad mykeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void WellcomeScreen() {
lcd.setCursor(2, 0);
lcd.print("I am M A Majeed");
lcd.setCursor(5,1);
lcd.print("This is my");
lcd.setCursor(1, 2);
String message = "Arduino Calculator";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(1000);
}
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
void setup() {
Serial.begin(115200);
lcd.begin(20, 4);
WellcomeScreen();
lcd.clear();
lcd.cursor();
lcd.setCursor(0, 0);
}
boolean presentValue = false;
boolean next = false;
boolean next1 = false;
boolean final = false;
String num1, num2,num3;
double answer;
char Operator1=' ';
char Operator2=' ';
int numLength1 =0;
int numLength2 =0;
int DpState = LOW;
void loop(){
// put your main code here, to run repeatedly:
char key =mykeypad.getKey();
if (key !=NO_KEY && (key== '1' ||key== '2' ||key== '3' ||key== '4' ||key== '5' ||key== '6' ||key== '7' ||key== '8' ||key== '9' ||key== '0' ))
{
Serial.print("op2: ");
Serial.println(Operator2);
Serial.print("op1: ");
Serial.println(Operator1);
if (presentValue !=true)
{
num1 += key;
lcd.setCursor(0,0);
lcd.print(num1);
Serial.print("num1: ");
Serial.println(num1);
numLength1 =num1.length();
}
else if (next ==true)
{
num2 += key;
//int numLength = num2.length();
lcd.setCursor(numLength1+1,0);
lcd.print(num2);
Serial.print("num2: ");
Serial.println(num2);
numLength2 =num2.length();
next1 = true;
}
else
{
num3 += key;
lcd.setCursor(numLength1+numLength2+2,0);
lcd.print(num3);
Serial.print("num3: ");
Serial.println(num3);
}
}
else if ( key !=NO_KEY && (key== '/' ||key== '*' ||key== '-' ||key== '+' ))
{
if (presentValue == false)
{
presentValue = true;
Operator1=key;
lcd.print(Operator1);
next= true;
Serial.println(next);
}
else if (next1 == true && key!= '=')
{
next = false;
Operator2=key;
Serial.print("op2: ");
Serial.println(Operator2);
Serial.print("op1: ");
Serial.println(Operator1);
lcd.print(Operator2);
next1= false;
}
}
else if ( key != NO_KEY && key == '=')
{
Serial.print("calop2: ");
Serial.println(Operator2);
Serial.print("calop1: ");
Serial.println(Operator1);
//calculation part for num1, num2
if (Operator2 ==' ')
{
switch (Operator1) {
case '*':
answer= num1.toDouble()* num2.toDouble();
break;
case '/':
answer= num1.toDouble()/ num2.toDouble();
break;
case '+':
answer= num1.toDouble()+ num2.toDouble();
break;
case '-':
answer= num1.toDouble()- num2.toDouble();
break;
}
}
else
//calculation part for num1,num2,num3
switch (Operator1) {
Serial.print("Operator2: ");
Serial.println(Operator2);
case '*':
{
switch (Operator2) {
case '*':
answer= num1.toDouble()* num2.toDouble()* num3.toDouble();
break;
case '/':
answer= num1.toDouble()* num2.toDouble()/ num3.toDouble();
break;
case '+':
answer= num1.toDouble()* num2.toDouble()+ num3.toDouble();
break;
case '-':
answer= num1.toDouble()* num2.toDouble()- num3.toDouble();
break;
}
}
break;
case '/':
{
switch (Operator2) {
case '*':
answer= num1.toDouble()/ num2.toDouble()* num3.toDouble();
break;
case '/':
answer= num1.toDouble()/ num2.toDouble()/ num3.toDouble();
break;
case '+':
answer= num1.toDouble()/ num2.toDouble()+ num3.toDouble();
break;
case '-':
answer= num1.toDouble()/ num2.toDouble()- num3.toDouble();
break;
}
}
break;
case '+':
switch (Operator2) {
case '*':
answer= num1.toDouble()+ num2.toDouble()* num3.toDouble();
break;
case '/':
answer= num1.toDouble()+ num2.toDouble()/ num3.toDouble();
break;
case '+':
answer= num1.toDouble()+ num2.toDouble()+ num3.toDouble();
break;
case '-':
answer= num1.toDouble()+ num2.toDouble()- num3.toDouble();
break;
}
break;
case '-':
switch (Operator2) {
case '*':
answer= num1.toDouble()- num2.toDouble()* num3.toDouble();
break;
case '/':
answer= num1.toDouble()- num2.toDouble()/ num3.toDouble();
break;
case '+':
answer= num1.toDouble()- num2.toDouble()+ num3.toDouble();
break;
case '-':
answer= num1.toDouble()- num2.toDouble()- num3.toDouble();
break;
}
break;
}
Serial.print("answer: ");
Serial.println(answer);
lcd.setCursor(0,3);
//lcd.autoscroll();
lcd.print(answer);
//lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'c'){
// toggle state of dp
DpState = !DpState;
if(DpState==HIGH)
{
lcd.noDisplay(); // turn off the display
//lcd.print("hello, world!");
lcd.noCursor(); // turn off the cursor
//lcd.clear();
}
else if(DpState==LOW)
{
lcd.display(); // turn on the display
lcd.cursor();
lcd.clear();
lcd.home(); // move the cursor to the upper-left corner of the LCD
presentValue = false;
final = false;
next = false;
next1 = false;
num1 = "";
num2 = "";
num3 = "";
answer=0;
Operator1 = ' ';
Operator2 = ' ';
}
}
}