#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int operand1 = 0; // First operand
int operand2 = 0; // Second operand
int result = 0; // Result of operation
char current_operator = '\0'; // Current operator
void setup() {
lcd.init();
lcd.backlight();
receiver.enableIRIn(); // Start the receiver
}
void loop() {
// Checks if received an IR signal
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
}
void translateIR() {
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 2:
setOperator('+');
break;
case 168: // PLAY button, used for calculation
calculateResult();
break;
case 104:
appendDigit(0);
break;
case 152:
setOperator('-');
break;
case 176:
clearOperands();
clearDisplay();
break;
case 48:
appendDigit(1);
break;
case 24:
appendDigit(2);
break;
case 122:
appendDigit(3);
break;
case 16:
appendDigit(4);
break;
case 56:
appendDigit(5);
break;
case 90:
appendDigit(6);
break;
case 66:
appendDigit(7);
break;
case 74:
appendDigit(8);
break;
case 82:
appendDigit(9);
break;
default:
lcd.clear();
lcd.print(receiver.decodedIRData.command);
lcd.print(" other button");
}
}
void appendDigit(int digit) {
if (current_operator == '\0') {
operand1 = operand1 * 10 + digit;
} else {
operand2 = operand2 * 10 + digit;
}
displayOperands();
}
void setOperator(char op) {
current_operator = op;
displayOperands();
}
void calculateResult() {
switch (current_operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
}
clearOperands();
displayResult();
current_operator = '\0'; // Reset operator
}
void displayOperands() {
lcd.setCursor(0, 0);
lcd.print("A: ");
lcd.print(operand1);
lcd.setCursor(8, 0);
lcd.print("B: ");
lcd.print(operand2);
lcd.setCursor(7, 0);
lcd.print(current_operator);
}
void displayResult() {
lcd.setCursor(0, 1);
lcd.print("Result: ");
lcd.print(result);
}
void clearOperands() {
operand1 = 0;
operand2 = 0;
}
void clearDisplay() {
lcd.clear();
}
void lcdPrint(const char* message) {
lcd.clear();
lcd.print(message);
}