Components and supplies
16x2 LCD display with I²C interface
Membrane Keypad
UNO R3
Tools and machines
uno
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
lighthouse projects from home
js
1#include <Keypad.h> 2#include <LiquidCrystal_I2C.h> 3 4// LCD setup 5LiquidCrystal_I2C lcd(0x27, 16, 2); 6 7// Pin assignments 8const int buzzerPin = A0; 9const int pirPin = 2; 10 11// PIN code (fixed for now) 12String storedPIN = "1234"; 13String inputCode = ""; 14 15// System states 16bool isArmed = false; 17bool disarmCountdown = false; 18bool exitDelayActive = false; 19 20unsigned long entryStartTime; 21unsigned long exitStartTime; 22const int entryDelay = 15000; // 15 seconds 23const int exitDelay = 10000; // 10 seconds 24 25// Timing for buzzer 26unsigned long lastBeepTime = 0; 27 28// Keypad setup 29const byte ROWS = 4; 30const byte COLS = 4; 31char keys[ROWS][COLS] = { 32 {'1','2','3','A'}, 33 {'4','5','6','B'}, 34 {'7','8','9','C'}, 35 {'*','0','#','D'} 36}; 37byte rowPins[ROWS] = {10, 9, 8, 7}; 38byte colPins[COLS] = {6, 5, 4, 3}; 39Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 40 41void beep(int duration = 100) { 42 tone(buzzerPin, 1000); 43 delay(duration); 44 noTone(buzzerPin); 45} 46 47void setup() { 48 Serial.begin(9600); 49 pinMode(pirPin, INPUT); 50 pinMode(buzzerPin, OUTPUT); 51 52 lcd.init(); 53 lcd.backlight(); 54 lcd.setCursor(0, 0); 55 lcd.print("Enter PIN:"); 56} 57 58void loop() { 59 char key = keypad.getKey(); 60 61 if (key) { 62 beep(); 63 handleKeyInput(key); 64 } 65 66 // Exit delay logic 67 if (exitDelayActive) { 68 unsigned long elapsed = millis() - exitStartTime; 69 if (elapsed >= exitDelay) { 70 isArmed = true; 71 exitDelayActive = false; 72 lcd.clear(); 73 lcd.setCursor(0, 0); 74 lcd.print("System ARMED"); 75 } else { 76 lcd.setCursor(0, 0); 77 lcd.print("Arming: "); 78 lcd.print((exitDelay - elapsed) / 1000); 79 lcd.print("s "); 80 81 if (millis() - lastBeepTime >= 1000) { 82 lastBeepTime = millis(); 83 beep(100); 84 } 85 } 86 } 87 88 // Entry delay logic 89 if (disarmCountdown) { 90 unsigned long elapsed = millis() - entryStartTime; 91 int remaining = (entryDelay - elapsed) / 1000; 92 93 if (elapsed >= entryDelay) { 94 disarmCountdown = false; 95 soundAlarm(); 96 } else { 97 lcd.setCursor(0, 0); 98 lcd.print("Motion Detected! "); 99 lcd.setCursor(0, 1); 100 lcd.print("Disarm: "); 101 lcd.print(remaining); 102 lcd.print("s "); 103 104 if (millis() - lastBeepTime >= 1000) { 105 lastBeepTime = millis(); 106 beep(100); 107 } 108 } 109 } 110 111 // PIR detection when armed 112 if (isArmed && digitalRead(pirPin) == HIGH && !disarmCountdown) { 113 disarmCountdown = true; 114 entryStartTime = millis(); 115 lastBeepTime = 0; 116 lcd.clear(); 117 } 118} 119 120void soundAlarm() { 121 lcd.clear(); 122 lcd.setCursor(0, 0); 123 lcd.print("!! ALARM !!"); 124 for (int i = 0; i < 10; i++) { 125 tone(buzzerPin, 2000); 126 delay(200); 127 noTone(buzzerPin); 128 delay(200); 129 } 130} 131 132void handleKeyInput(char key) { 133 if (key == '#') { 134 checkPIN(); 135 } else if (key == '*') { 136 inputCode = ""; 137 lcd.clear(); 138 lcd.setCursor(0, 0); 139 lcd.print("PIN cleared"); 140 } else if (inputCode.length() < 4 && isDigit(key)) { 141 inputCode += key; 142 lcd.setCursor(0, 0); 143 lcd.print("Code: "); 144 lcd.print(inputCode); 145 lcd.print(" "); 146 } 147} 148 149void checkPIN() { 150 if (inputCode == storedPIN) { 151 // Disarm system in any state 152 if (disarmCountdown || exitDelayActive || isArmed) { 153 disarmCountdown = false; 154 exitDelayActive = false; 155 isArmed = false; 156 lcd.clear(); 157 lcd.setCursor(0, 0); 158 lcd.print("System DISARMED"); 159 } else { 160 lcd.clear(); 161 lcd.setCursor(0, 0); 162 lcd.print("Arming in 10s..."); 163 exitDelayActive = true; 164 exitStartTime = millis(); 165 lastBeepTime = 0; 166 } 167 } else { 168 lcd.clear(); 169 lcd.setCursor(0, 0); 170 lcd.print("Wrong PIN!"); 171 } 172 173 inputCode = ""; 174}
Comments
Only logged in users can leave comments