#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin numbers for the buttons
const int buttonPin1 = 2; // Change these pin numbers according to your setup
const int buttonPin2 = 3;
const int buttonPin3 = 4;
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
// Variables to store button states and timings
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
// Variables to store button values
int value1 = 0;
int value2 = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Set up the button pins as inputs:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// Initialize serial communication:
Serial.begin(9600);
}
void loop() {
// Read the state of the buttons
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// Check if any two buttons are pressed and store their values
if (buttonState1 == HIGH && buttonState2 == HIGH) {
value1 = 1;
value2 = 2;
delay(200); // Debouncing delay
} else if (buttonState2 == HIGH && buttonState3 == HIGH) {
value1 = 2;
value2 = 3;
delay(200); // Debouncing delay
} else if (buttonState3 == HIGH && buttonState1 == HIGH) {
value1 = 3;
value2 = 1;
delay(200); // Debouncing delay
}
// If two buttons are pressed within 2 seconds, calculate and print the result
if (value1 != 0 && value2 != 0) {
int result = value1 * value2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Result: ");
lcd.print(result);
Serial.print("Result: ");
Serial.println(result);
delay(2000); // Display the result for 2 seconds
value1 = 0; // Reset value1
value2 = 0; // Reset value2
}
}