#include <LiquidCrystal.h> // include the LCD library
#include <Keypad.h> // include the keypad library
// initialize the LCD display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// initialize the keypad
const byte ROWS = 4; // number of rows on the keypad
const byte COLS = 4; // number of columns on the keypad
char keys[ROWS][COLS] = { // define the keypad layout
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect the row pins of the keypad to these Arduino pins
byte colPins[COLS] = {13, 10, A0, A1}; // connect the column pins of the keypad to these Arduino pins
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // initialize the keypad with the defined layout and pins
// define the fixed pin
const int fixedPin = 1234; // change this to your desired fixed pin
void setup() {
// initialize the LCD display
lcd.begin(16, 2);
// print a welcome message on the LCD
lcd.print("Welcome!");
// set the cursor to the second line
lcd.setCursor(0, 1);
// print instructions on the LCD
lcd.print("Enter pin:");
}
void loop() {
// get input from the keypad
char key = keypad.getKey();
// check if a key is pressed
if (key) {
// check if the key is a number
if (key >= '0' && key <= '9') {
// add the number to the pin
int pin = key - '0';
// print the number on the LCD
lcd.print(key);
// wait for 500 milliseconds
delay(500);
}
// check if the key is the special character
else if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
// check if the entered pin is correct
if (pin == fixedPin) {
// print the special character on the LCD
lcd.print("!");
// wait for 500 milliseconds
delay(500);
}
else {
// print an error message on the LCD
lcd.print("Incorrect pin!");
// wait for 500 milliseconds
delay(500);
// clear the LCD display
lcd.clear();
// print instructions on the LCD
lcd.print("Enter pin:");
}
}
}
}
// This code was adapted from the following sources:
// - LiquidCrystal library examples: https://www.arduino.cc/en/Reference/LiquidCrystal
// - Keypad library examples: https://www.arduino.cc/en/Tutorial/Keypad