#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
lcd.clear();
lcd.print("Kalkulator IR");
delay(2000); // Display for 2 seconds before starting
lcd.clear();
}
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: // Example: "+" button
setOperator('+');
break;
case 168: // Example: "PLAY" button to calculate the result
calculateResult();
break;
case 48: // Example: Button "1"
appendDigit(1);
break;
case 24: // Example: Button "2"
appendDigit(2);
break;
case 122: // Example: Button "3"
appendDigit(3);
break;
case 16: // Example: Button "4"
appendDigit(4);
break;
case 56: // Example: Button "5"
appendDigit(5);
break;
case 90: // Example: Button "6"
appendDigit(6);
break;
case 66: // Example: Button "7"
appendDigit(7);
break;
case 74: // Example: Button "8"
appendDigit(8);
break;
case 82: // Example: Button "9"
appendDigit(9);
break;
case 104: // Example: Button "0"
appendDigit(0);
break;
case 176: // Example: Button to clear (reset)
clearOperands();
clearDisplay();
break;
default:
lcd.clear();
lcd.print("Unknown Code");
}
}
void appendDigit(int digit) {
if (current_operator == '\0') {
operand1 = operand1 * 10 + digit; // Building the first operand
} else {
operand2 = operand2 * 10 + digit; // Building the second operand
}
displayOperands(); // Update display with the current operands
}
void setOperator(char op) {
current_operator = op; // Set the operator ('+')
displayOperands();
}
void calculateResult() {
if (current_operator == '+') {
result = operand1 + operand2; // Perform addition
}
clearOperands(); // Clear operands after calculating the result
displayResult();
current_operator = '\0'; // Reset the operator
}
void displayOperands() {
lcd.clear();
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();
}