#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Define I2C LCD address and dimensions
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define GPIO pins
#define TRIG_PIN 12 // GPIO pin connected to the Trig pin of the ultrasonic sensor
#define ECHO_PIN 13 // GPIO pin connected to the Echo pin of the ultrasonic sensor
#define RED_LED_PIN 14 // GPIO pin connected to the red LED
#define GREEN_LED_PIN 15 // GPIO pin connected to the green LED
#define BUTTON_PIN 26 // GPIO pin for the submit button
#define BUZZER_PIN 25
// Correct PIN
const String correctPin = "123456";
// Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 4, 0, 2}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 17, 18, 19}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String enteredPin = "";
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set up pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome");
}
// Function to measure distance
float measureDistance() {
// Send a 10us pulse to trigger the ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
float distance = (duration * 0.0343) / 2; // Speed of sound = 343 m/s or 0.0343 cm/us
return distance;
}
void loop() {
float distance = measureDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance <= 10 && distance > 0) {
// Object detected within range
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Pin:");
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
char key = keypad.getKey();
if (key) {
if (key == '*') {
// Clear entered PIN
enteredPin = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Pin:");
} else if (key == '#') {
// Submit PIN
if (enteredPin == correctPin) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thank You");
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green LED
delay(2000); // Show message for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome");
enteredPin = "";
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pin Wrong");
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
delay(2000); // Show message for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Pin:");
enteredPin = "";
}
} else {
// Add digit to entered PIN
if (enteredPin.length() < 6) {
enteredPin += key;
lcd.setCursor(0, 1);
lcd.print(enteredPin);
}
}
}
} else {
// No object detected
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome");
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED
}
delay(100); // Small delay to prevent rapid re-triggering
}