#define delay_time 500
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
volatile char keypad_num[4][8] = {
{'1', '2', '0', '3', '0', '0', '0', '+'},
{'4', '5', '0', '6', '0', '0', '0', '-'},
{'7', '8', '0', '9', '0', '0', '0', '*'},
{'C', '0', '0', '=', '0', '0', '0', '/'}
};
LiquidCrystal_I2C lcd(0x27, 16, 2);
void keypad_input_calculation();
void calculation(char *str);
void setup() {
volatile unsigned char *dirf, *dirk, *outf, *ink ;
dirk = (volatile char *)0x107; *dirk = 0x00;
dirf = (volatile char *)0x30; *dirf = 0xff;
lcd.init();
lcd.backlight();
}
void loop() {
keypad_input_calculation();
delay(delay_time);
}
void keypad_input_calculation() {
volatile unsigned char *dirf, *dirk, *outf, *ink;
dirk = (volatile unsigned char *)0x107; *dirk = 0x00;
dirf = (volatile unsigned char *)0x30; *dirf = 0xff;
outf = (volatile unsigned char *)0x31;
ink = (volatile unsigned char *)0x106;
int j = 0;
int k =0;
char buffer[16] = {0};
while (1) {
for (volatile long i = 0; i < 4; i++) {
*outf = 1 << i;
volatile unsigned char y = *ink;
if (y != 0) {
if (i == 3 && y == 1) {//clear the calculator
lcd.clear();
j = 0;
memset(buffer, '\0', sizeof(buffer)); // Clear the buffer
}
else if (i == 3 && y == 4) {//= krne k liye
lcd.setCursor(0, 1);
calculation(buffer);
}
else{//to take input and print on the display
buffer[j]= keypad_num[i][y-1];
buffer[j+1]='\0';
j++;
delay(delay_time);
while(buffer[k]!='\0'){//priting the string
lcd.setCursor(0,0);
lcd.print(buffer);
k++;
}
}
}
}
}
}
void calculation(char *str) {
char t_str1[16] = {0}, t_str2[16] = {0};
char temp_opr;
long num1, num2;
float ans;
unsigned char i = 0, j = 0;
// Extract the first number and operator
while (str[i] != '+' && str[i] != '*' && str[i] != '-' && str[i] != '/' && str[i] != '\0') {
t_str1[i] = str[i];
i++;
}
t_str1[i] = '\0';
num1 = atol(t_str1);
// Extract the operator
temp_opr = str[i];
i++;
memset(t_str1,'\0',16);
// Extract the second number
while (str[i] != '\0') {
t_str1[j] = str[i];
i++;
j++;
}
t_str1[j] = '\0';
// Convert extracted strings to numbers
num2 = atol(t_str1);
// Perform the calculation based on the operator
switch (temp_opr) {
case '+':
lcd.setCursor(0, 1);
lcd.print(num1+num2);
break;
case '-':
lcd.setCursor(0, 1);
lcd.print(num1-num2);
break;
case '*':
lcd.setCursor(0, 1);
lcd.print(num1*num2);
break;
case '/':
lcd.setCursor(0, 1);
lcd.print((float)num1/num2);
break;
default:
ans = 0;
break;
}
// Print the result on the LCD
}