Devices & Components
10 jumper wires 150mm male
16x2 LCD display with I²C interface
Breadboard - 830 contacts
Arduino Uno Rev3
5 Volt Yellow LED
Green LED
Push Button
5 Volt Red LED
Software & Tools
Arduino IDE
Project description
Code
Security Access System
js
Last Updated: 9/28/2023
1#include <LiquidCrystal.h> 2 3// Initialize the LiquidCrystal library with the LCD pins 4LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // RS, E, D4, D5, D6, D7 5 6// Define pins for LEDs 7const int greenLedPin = A3; 8const int yellowLedPin = A4; 9const int redLedPin = A5; 10 11// Define pins for buttons 12const int buttonAPin = 2; 13const int buttonBPin = 3; 14const int buttonCPin = 4; 15const int buttonDPin = 5; 16 17// Code sequence to disarm the alarm (press 'A' four times) 18const char disarmCode[] = "AAAA"; 19const int disarmCodeLength = 4; 20int disarmCodeIndex = 0; 21 22// Countdown timer variables 23unsigned long previousMillis = 0; 24const long interval = 1000; // 1 second 25int seconds = 15; // Change to 15 seconds 26boolean alarmActive = true; 27 28// Delay between button 'A' presses 29unsigned long buttonDelayMillis = 500; // Adjust as needed 30 31void setup() { 32 // Initialize the LCD with 16 columns and 2 rows 33 lcd.begin(16, 2); 34 35 // Display initial time on the LCD 36 lcd.print("TIME: 15"); 37 38 // Set the pinMode for LEDs and buttons 39 pinMode(greenLedPin, OUTPUT); 40 pinMode(yellowLedPin, OUTPUT); 41 pinMode(redLedPin, OUTPUT); 42 pinMode(buttonAPin, INPUT_PULLUP); 43 pinMode(buttonBPin, INPUT_PULLUP); 44 pinMode(buttonCPin, INPUT_PULLUP); 45 pinMode(buttonDPin, INPUT_PULLUP); 46} 47 48void loop() { 49 // Check for button presses 50 if (digitalRead(buttonAPin) == LOW) { 51 checkButton('A'); 52 } 53 if (digitalRead(buttonBPin) == LOW) { 54 checkButton('B'); 55 } 56 if (digitalRead(buttonCPin) == LOW) { 57 checkButton('C'); 58 } 59 if (digitalRead(buttonDPin) == LOW) { 60 checkButton('D'); 61 } 62 63 // Update the countdown timer 64 unsigned long currentMillis = millis(); 65 if (alarmActive && currentMillis - previousMillis >= interval) { 66 previousMillis = currentMillis; 67 seconds--; 68 69 // Check if seconds is negative 70 if (seconds < 0) { 71 // Timer expired, activate red LED and display "DENIED" 72 alarmActive = false; 73 digitalWrite(redLedPin, HIGH); 74 lcd.clear(); 75 lcd.setCursor(0, 0); 76 lcd.print("Access DENIED"); 77 delay(2000); // Display "Access DENIED" for 2 seconds 78 lcd.clear(); 79 lcd.setCursor(0, 0); 80 lcd.print("TIME: 15"); // Reset timer to 15 seconds 81 seconds = 15; 82 disarmCodeIndex = 0; // Reset code input 83 } 84 } 85 86 // Display countdown and code on LCD 87 lcd.setCursor(6, 0); 88 lcd.print(seconds < 10 ? " " : ""); // Add a leading space if seconds is single-digit 89 lcd.print(seconds); 90 91 // Check if the correct code was entered 92 if (disarmCodeIndex == disarmCodeLength) { 93 alarmActive = false; 94 digitalWrite(greenLedPin, HIGH); 95 lcd.clear(); 96 lcd.setCursor(0, 0); 97 lcd.print("Access GRANTED"); 98 delay(2000); // Display "Access GRANTED" for 2 seconds 99 lcd.clear(); 100 lcd.setCursor(0, 0); 101 lcd.print("TIME: 15"); // Reset timer to 15 seconds 102 seconds = 15; 103 disarmCodeIndex = 0; // Reset code input 104 } 105} 106 107void checkButton(char button) { 108 if (button == disarmCode[disarmCodeIndex]) { 109 disarmCodeIndex++; 110 if (disarmCodeIndex == disarmCodeLength) { 111 // Correct code entered, grant access 112 alarmActive = false; 113 digitalWrite(greenLedPin, HIGH); 114 lcd.clear(); 115 lcd.setCursor(0, 0); 116 lcd.print("Access GRANTED"); 117 delay(2000); // Display "Access GRANTED" for 2 seconds 118 lcd.clear(); 119 lcd.setCursor(0, 0); 120 lcd.print("TIME: 15"); // Reset timer to 15 seconds 121 seconds = 15; 122 disarmCodeIndex = 0; // Reset code input 123 } else { 124 // Delay between button presses 125 delay(buttonDelayMillis); 126 } 127 } else { 128 // Incorrect code, reset code input 129 disarmCodeIndex = 0; 130 digitalWrite(yellowLedPin, HIGH); 131 delay(1000); // Display "Incorrect" for 1 second 132 digitalWrite(yellowLedPin, LOW); 133 } 134}
Documentation
Security Access System
Access Granted
file.None

Security Access System
Access Denied
file.None

Comments
Only logged in users can leave comments