#include <Wire.h> // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include LCD library for I2C
#include <Keypad.h> // Include Keypad library
// Initialize LCD with I2C address (0x27 is common, change if needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte row = 4; // Define the number of rows in the keypad
const byte col = 4; // Define the number of columns in the keypad
// Define the keypad layout (characters assigned to each button)
char keyarray[row][col] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define column and row pin connections for the keypad
byte cpins[col] = {2, 3, 4, 5}; // Column pinouts (C4, C3, C2, C1)
byte rpins[row] = {6, 7, 8, 9}; // Row pinouts (R4, R3, R2, R1)
// Initialize the keypad object with the defined layout and pins
Keypad kpd = Keypad(makeKeymap(keyarray), rpins, cpins, row, col);
void setup() {
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on backlight
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Press 1-9"); // Display message
lcd.setCursor(0, 1); // Set cursor to the second row, first column
lcd.print("For Multiplication"); // Display message
}
void loop() {
char key = kpd.getKey(); // Read key press from the keypad
if (key) { // Check if a key is pressed
lcd.clear(); // Clear LCD screen before displaying new content
if (key >= '1' && key <= '9') { // Check if the key pressed is between '1' and '9'
int num = key - '0'; // Convert the character to an integer
// Display multiplication table in pairs (x1 & x2, x3 & x4, etc.)
lcd.setCursor(0, 0);
lcd.print(num);
lcd.print("x1=");
lcd.print(num * 1);
lcd.setCursor(0, 1);
lcd.print(num);
lcd.print("x2=");
lcd.print(num * 2);
delay(1500); // Pause before updating
lcd.clear(); // Clear the screen for the next set of calculations
lcd.setCursor(0, 0);
lcd.print(num);
lcd.print("x3=");
lcd.print(num * 3);
lcd.setCursor(0, 1);
lcd.print(num);
lcd.print("x4=");
lcd.print(num * 4);
delay(1500); // Pause before updating
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(num);
lcd.print("x5=");
lcd.print(num * 5);
lcd.setCursor(0, 1);
lcd.print(num);
lcd.print("x6=");
lcd.print(num * 6);
delay(1500); // Pause before updating
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(num);
lcd.print("x7=");
lcd.print(num * 7);
lcd.setCursor(0, 1);
lcd.print(num);
lcd.print("x8=");
lcd.print(num * 8);
delay(1500); // Pause before updating
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(num);
lcd.print("x9=");
lcd.print(num * 9);
lcd.setCursor(0, 1);
lcd.print(num);
lcd.print("x10=");
lcd.print(num * 10);
delay(2000); // Pause before resetting the screen
// Reset LCD screen to default message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press 1-9");
lcd.setCursor(0, 1);
lcd.print("For Multiplication");
}
else if (key == '0') { // If '0' is pressed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Zero Selected"); // Display message
delay(1000); // Wait before clearing the screen
lcd.clear(); // Clear screen
}
}
}