#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad setup
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'#', '0', '*', 'D'}
};
byte rowPins[rows] = {13, 12, 14, 27}; // Row pins connected to these Arduino pins
byte colPins[cols] = {26, 25, 33, 32}; // Column pins connected to these Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
// LED Pins
const int greenLEDPin = 15;
const int yellowLEDPin = 16;
const int redLEDPin = 17;
// Push button pin
const int pushButtonPin = 4; // Connected to pin 4 of ESP32
// Touch sensor pins
const int touchSDAPin = 21;
const int touchSCLPin = 22;
void setup() {
// LCD setup
lcd.init();
lcd.backlight();
// LED setup
pinMode(greenLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
// Push button setup
pinMode(pushButtonPin, INPUT_PULLUP);
// Touch sensor setup
Wire.begin(touchSDAPin, touchSCLPin);
}
void loop() {
while (true) {
// Display waiting message and turn on green LED
displayWaitingMessage();
// Wait for input from push button or touch sensor
waitForInput();
// Once input is received, indicate processing with yellow LED
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
// Display touch sensed message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Touch sensed!");
lcd.setCursor(0, 1);
lcd.print("Let's start...");
delay(2000); // Display for 2 seconds
// Display calculator message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calculator:");
// Perform keypad operation
String input = "";
char key = getKey();
while (key != '*') {
if (key != NO_KEY) {
if (key == 'A') {
input += "+"; // Addition
} else if (key == 'B') {
input += "-"; // Subtraction
} else if (key == 'C') {
input += "*"; // Multiplication
} else if (key == 'D') {
input += "/"; // Division
} else if (key == '#') {
input = ""; // Clear input
} else {
input += key; // Numeric input
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calculator:");
lcd.setCursor(0, 1);
lcd.print(input);
}
key = getKey();
delay(100);
}
// Calculate and display result
float result = calculateResult(input);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Result: ");
lcd.print(result, 2); // Display result with 2 decimal places
// Turn on red LED
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW); // Turn off yellow LED during result display
// Display result for 3 seconds
delay(3000);
// Turn off red LED
digitalWrite(redLEDPin, LOW);
}
}
char getKey() {
char key = keypad.getKey();
return key;
}
void waitForInput() {
unsigned long startTime = millis();
while (true) {
if (keypad.getKey() != NO_KEY || digitalRead(pushButtonPin) == LOW || touchSensorTouched()) {
return;
}
// Check if waiting period exceeds 3 seconds
if (millis() - startTime > 3000) {
digitalWrite(greenLEDPin, LOW); // Turn off green LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Sensation!!!");
digitalWrite(redLEDPin, HIGH); // Turn on red LED
delay(3000); // Display message and keep red LED on for 3 seconds
digitalWrite(redLEDPin, LOW); // Turn off red LED
startTime = millis(); // Reset start time to keep displaying waiting message
displayWaitingMessage(); // Re-display waiting message
}
delay(100); // Check input every 100 milliseconds
}
}
void displayWaitingMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting...");
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
}
bool touchSensorTouched() {
// Check if touch sensor is touched
// Implement your touch sensor logic here
return false; // Placeholder, replace with actual logic
}
float calculateResult(String input) {
// Calculate result based on input string
float result = 0.0;
// Implement your calculation logic here
// For example:
result = eval(input);
return result;
}
float eval(String expr) {
// Basic expression evaluator
// Assumes expr is a valid arithmetic expression without parentheses
// Supports only +, -, *, /
int i = 0;
float leftOperand = 0.0;
float rightOperand = 0.0;
char op;
while (i < expr.length()) {
if (isdigit(expr[i]) || expr[i] == '.') {
leftOperand = expr.substring(i).toFloat();
break;
}
i++;
}
i++;
while (i < expr.length()) {
if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') {
op = expr[i];
break;
}
i++;
}
i++;
while (i < expr.length()) {
if (isdigit(expr[i]) || expr[i] == '.') {
rightOperand = expr.substring(i).toFloat();
break;
}
i++;
}
switch (op) {
case '+':
return leftOperand + rightOperand;
case '-':
return leftOperand - rightOperand;
case '*':
return leftOperand * rightOperand;
case '/':
if (rightOperand != 0) {
return leftOperand / rightOperand;
} else {
return 0.0; // Division by zero
}
default:
return 0.0; // Invalid operator
}
}