#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Setup LCD display (address, columns, rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define push button pin
const int buttonPin = 1;
// Setup keypad matrix
#define ROWS 4
#define COLS 4
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {A3, A2, A1, A0};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Function prototypes
void calculator();
void timer();
// Interrupt service routine
void modeSwitch() {
static bool mode = 0; // 0 = calculator, 1 = timer
mode = !mode;
lcd.clear();
if (mode) {
timer();
} else {
calculator();
}
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
// Attach interrupt to pin D1
attachInterrupt(digitalPinToInterrupt(buttonPin), modeSwitch, RISING);
sei(); // Enable interrupts
// Set up push button pin
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
char key = keypad.getKey();
if (key) {
switch (key) {
case '1':
// Calculator mode
calculator();
break;
case '2':
// Timer mode
timer();
break;
default:
break;
}
}
// Read the state of the push button
int buttonState = digitalRead(buttonPin);
if (buttonState ==LOW) {
// Button is pressed
// Call modeSwitch() function to toggle between calculator and timer modes
modeSwitch();
}
}
void calculator() {
lcd.setCursor(0, 0);
lcd.print("Calculator Mode");
char input[17] = "";
char operation = '\0';
float result;
float num1, num2;
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '.') {
break;
}
if (isdigit(key) || key == '.') {
strncat(input, &key, 1);
lcd.setCursor(0, 1);
lcd.print(input);
} else if (strchr("+-*/", key)) {
if (strlen(input) > 0 && operation == '\0') {
// If an operand has been pressed for the first time,
// store the current input as num1 and display it on the same line
// as the operand.
operation = key;
num1 = atof(input);
input[0] = '\0';
lcd.setCursor(0, 1);
lcd.print(num1);
lcd.print(operation);
} else if (strlen(input) > 0 && operation != '\0') {
// If an operand has been pressed for the second time,
// store the current input as num2, perform the calculation,
// and display the result on the next line.
num2 = atof(input);
switch (operation) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default: break;
}
num1 = result;
input[0] = '\0';
lcd.setCursor(0, 1);
lcd.print(num2);
lcd.print(operation);
lcd.setCursor(0, 0);
lcd.print(result, 2);
}
} else if (key == '=') {
// If the equal sign is pressed, perform the calculation and
// display the result on the next line.
num2 = atof(input);
switch (operation) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default: break;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(result, 2);
input[0] = '\0';
operation = '\0';
}
}
}
lcd.clear();
}
volatile int remainingTime = 0;
volatile bool timerRunning = false;
void timerISR() {
// Decrement the remaining time if the timer is running
if (timerRunning) {
remainingTime--;
}
}
void timer() {
timerRunning = false;
lcd.setCursor(0, 0);
lcd.print("Timer Mode");
char input[17] = "";
int duration = 0;
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '.') {
break;
}
if (isdigit(key)) {
strncat(input, &key, 1);
lcd.setCursor(0, 1);
lcd.print(input);
duration = atoi(input);
} else if (key == '=') {
// Start the timer
timerRunning = true;
remainingTime = duration;
input[0] = '\0';
}
}
// Display the remaining time
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(remainingTime);
// Check if the timer has expired
if (remainingTime <= 0) {
timerRunning = false;
// Reset the input and display a message
input[0], '\0';
lcd.setCursor(0, 1);
lcd.print("Time's up!");
delay(1000);
lcd.clear();
}
}
}