#include <Arduino.h> // Include the Arduino library for standard functions
// Define GPIO pins for various components
#define BUZZER_PIN 14 // GPIO pin for the buzzer
#define GREEN_LED 26 // GPIO pin for the green LED (access granted indicator)
#define RED_LED 25 // GPIO pin for the red LED (access denied indicator)
#define RELAY_PIN 27 // GPIO pin for controlling the relay (door mechanism)
void setup() {
Serial.begin(115200); // Initialize serial communication at 115200 baud rate
// Set the defined GPIO pins as output
pinMode(BUZZER_PIN, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
// Set initial state: turn off buzzer, LEDs, and relay
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW);
digitalWrite(RELAY_PIN, LOW);
// Prompt user to enter an e-KTP ID
Serial.println("Enter e-KTP ID (format: XX XX XX XX):");
}
void loop() {
// Check if there is any incoming serial data
if (Serial.available()) {
String input = Serial.readStringUntil('\n'); // Read the input until a newline character is received
input.trim(); // Remove leading and trailing whitespace
// Validate the format of the entered ID
if (isValidFormat(input)) {
// Check if the entered ID matches the registered ID
if (input.equals("12 34 56 78")) { // Replace with the correct registered e-KTP ID
accessGranted(); // Call function to grant access
} else {
accessDenied(); // Call function to deny access
}
} else {
// Print an error message if the input format is incorrect
Serial.println("Invalid format. Enter e-KTP ID (format: XX XX XX XX):");
}
}
}
// Function to validate if input follows the "XX XX XX XX" format
bool isValidFormat(String input) {
// Check if the length matches the expected format (11 characters including spaces)
if (input.length() == 11 && input.charAt(2) == ' ' && input.charAt(5) == ' ' && input.charAt(8) == ' ') {
// Loop through each character to ensure valid digits in the correct places
for (int i = 0; i < input.length(); i++) {
if (i != 2 && i != 5 && i != 8) { // Ignore spaces at positions 2, 5, and 8
if (!isDigit(input.charAt(i))) { // Check if character is a digit
return false; // Return false if a non-digit is found
}
}
}
return true; // Return true if the format is valid
}
return false; // Return false if length or spacing is incorrect
}
// Function to handle access granted scenario
void accessGranted() {
Serial.println("Access granted!"); // Print access granted message
// Activate buzzer, green LED, and relay (door unlocking mechanism)
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RELAY_PIN, HIGH);
delay(5000); // Keep buzzer and LED on for 5 seconds
// Turn off buzzer, green LED, and relay after delay
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(RELAY_PIN, LOW);
}
// Function to handle access denied scenario
void accessDenied() {
Serial.println("Access denied!"); // Print access denied message
// Activate buzzer and red LED to indicate denial
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(RED_LED, HIGH);
delay(5000); // Keep buzzer and LED on for 5 seconds
// Turn off buzzer and red LED after delay
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RED_LED, LOW);
}