#include "Adafruit_Keypad.h"
#include <LiquidCrystal_I2C.h>
// Initialize LCD with I2C address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 4;
int led = 12; // LED connected to pin 1
int buz = 13; // Buzzer connected to pin 0
// Define keypad layout
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define row and column pins
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {11, 10, 9, 8};
// Initialize keypad
Adafruit_Keypad customKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
customKeypad.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Press a key:");
pinMode(led, OUTPUT);
pinMode(buz, OUTPUT);
}
void loop() {
customKeypad.tick();
while (customKeypad.available()) {
keypadEvent e = customKeypad.read();
if (e.bit.EVENT == KEY_JUST_PRESSED) {
char key = (char)e.bit.KEY;
Serial.print("Key Pressed: ");
Serial.println(key);
// Display the key on the LCD
lcd.setCursor(0, 1);
lcd.print("Key: ");
lcd.print(key);
lcd.print(" "); // Clear extra characters
// Key "1" → Blink LED 3 times
if (key == '1') {
for (int i = 0; i < 3; i++) {
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
}
}
// Key "2" → Buzzer sound at 1200Hz for 200ms, off for 200ms
else if (key == '2') {
tone(buz, 1200); // Play tone at 1200Hz
delay(200);
noTone(buz); // Stop tone
delay(200);
}
}
}
delay(10);
}