Untitled
unknown
plain_text
a year ago
13 kB
6
Indexable
#include <LiquidCrystal.h> // Include library for LCD display
//* keypad pin y4 to pico digital pin gp16 // Connect y4 pin to GP16
//* keypad pin y3 to pico digital pin gp17 // Connect y3 pin to GP17
//* keypad pin y2 to pico digital pin gp18 // Connect y2 pin to GP18
//* keypad pin y1 to pico digital pin gp19 // Connect y1 pin to GP19
//* keypad pin x4 to pico digital pin gp10 // Connect x4 pin to GP10
//* keypad pin x3 to pico digital pin gp11 // Connect x3 pin to GP11
//* keypad pin x2 to pico digital pin gp12 // Connect x2 pin to GP12
//* keypad pin x1 to pico digital pin gp13 // Connect x1 pin to GP13
// LCD pin 1 to pico GND // Ground connection for LCD
// LCD pin 2 to pico SV VBUS // Power supply to LCD
// LCD pin 3 to pico GND // Contrast pin grounded
// LCD pin 4 (RS) to pico GP2 // Register select to GP2
// LCD pin 5 RW to GND // Read/write mode to ground
// LCD pin 6 EN to pico GP3 // Enable pin to GP3
// LCD pin 11 (D4) to pico GP4 // Data pin D4 to GP4
// LCD pin 12 (D5) to pico GP5 // Data pin D5 to GP5
// LCD pin 13 (D6) to pico GP6 // Data pin D6 to GP6
// LCD pin 14 (D7) to pico GP7 // Data pin D7 to GP7
// Button 1 -> GPIO 14 (emulating door/window sensor) // Connect button to GPIO 14
// Button 2 -> GPIO 15 (emulating motion sensor) // Connect button to GPIO 15
// LED: Connected to GPIO 1. // LED connected for alarm indication
// Buzzer: Connected to GPIO 0. // Buzzer connected for sound alert
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Initialize LCD with pin configuration
// Keypad pin configurations
int rowPins[4] = {10, 11, 12, 13}; // Rows connected to GPIO 10-13
int colPins[4] = {16, 17, 18, 19}; // Columns connected to GPIO 16-19
// LED and Buzzer configurations
const int button1Pin = 14; // Sensor 1 (Door/Window) input pin
const int button2Pin = 15; // Sensor 2 (Motion) input pin
const int buzzerPin = 0; // Buzzer output pin
const int ledPin = 1; // LED output pin
// Variables
String enteredCode = ""; // Store entered keypad code
String correctCode = "1234"; // Set correct code to "1234"
int incorrectAttempts = 0; // Count incorrect code attempts
bool isArmed = false; // System armed status
bool keyPressed[4][4] = {{false}}; // Track key press states
bool keyPreviouslyPressed = false; // Prevent key repeat issues
char lastKeyPressed = '\0'; // Store last key pressed
bool allowPasswordChange = true; // Enable password change
unsigned long lastDebounceTime = 0; // Last debounce timestamp
const unsigned long debounceDelay = 200; // Debounce delay in milliseconds
unsigned long alarmStartTime = 0; // Alarm start time
bool alarmActive = false; // Track alarm status
void setup() {
// Initialize LCD
lcd.begin(16, 2); // Set LCD size to 16x2
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Ready"); // Display "Ready" on LCD
delay(2000); // Wait 2 seconds
lcd.clear(); // Clear LCD screen
lcd.print("Press A to ARM"); // Display arming message
delay(2000); // Wait 2 seconds
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Press D to"); // Display disarm message
lcd.setCursor(0, 1); // Move cursor to next line
lcd.print("DISARM"); // Display "DISARM"
delay(2000); // Wait 2 seconds
lcd.clear();
lcd.print("Press C to"); // Display password change prompt
delay(3000); // Wait 3 seconds
lcd.clear();
lcd.print("clear passcode"); // Prompt for clearing passcode
delay(1000); // Wait 1 second
lcd.clear();
lcd.print("change passcode"); // Prompt for passcode change
delay(1000); // Wait 1 second
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Check Windows"); // Display check windows message
lcd.setCursor(0, 1); // Move cursor to next line
lcd.print("Check Sensors"); // Display check sensors message
// Set pin modes for rows and columns
for (int i = 0; i < 4; i++) {
pinMode(rowPins[i], OUTPUT); // Set row pins as outputs
pinMode(colPins[i], INPUT_PULLUP); // Set column pins as inputs
digitalWrite(rowPins[i], HIGH); // Set rows to HIGH initially
}
// Set pin modes for sensors, LED, and buzzer
pinMode(button1Pin, INPUT); // Set button1 pin as input
pinMode(button2Pin, INPUT); // Set button2 pin as input
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
int readKeypad() {
for (int row = 0; row < 4; row++) {
digitalWrite(rowPins[row], LOW); // Set current row LOW
for (int col = 0; col < 4; col++) {
if (digitalRead(colPins[col]) == LOW) { // Check if button pressed
unsigned long currentTime = millis(); // Get current time
if ((currentTime - lastDebounceTime) > debounceDelay) { // Debounce check
if (!keyPressed[row][col]) { // Ensure key was not pressed before
keyPressed[row][col] = true; // Register key press
lastDebounceTime = currentTime; // Update debounce timestamp
digitalWrite(rowPins[row], HIGH); // Reset row to HIGH
return row * 4 + col; // Return key index value
}
}
} else {
keyPressed[row][col] = false; // Reset key press state
}
}
digitalWrite(rowPins[row], HIGH); // Reset row to HIGH
}
return -1; // Return -1 if no key pressed
}
void loop() {
int keyIndex = readKeypad(); // Read keypad input
if (keyIndex != -1 && !alarmActive) { // Allow keypad interaction only if alarm is not active
char keyMap[16] = {'1', '2', '3', 'F',
'4', '5', '6', 'E',
'7', '8', '9', 'D',
'A', '0', 'B', 'C'}; // Define key mapping
char key = keyMap[keyIndex]; // Get the pressed key
if (key != lastKeyPressed || !keyPreviouslyPressed) { // Check for key change
keyPreviouslyPressed = true; // Set key pressed status
lastKeyPressed = key; // Update last key pressed
// Allow only A, D, or C; abort on any other key
if (key != 'A' && key != 'D' && key != 'C' && !isdigit(key)) {
lcd.clear(); // Clear LCD screen
lcd.print("Invalid Key!"); // Display invalid key message
delay(1000); // Wait 1 second
lcd.clear(); // Clear LCD screen
enteredCode = ""; // Reset entered code
lcd.print("Enter Code:"); // Prompt for code entry
return; // Exit function early
}
if (isdigit(key) && enteredCode.indexOf(key) == -1) { // Check if key is numeric
enteredCode += key; // Append key to entered code
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Code: "); // Display "Code:" message
for (int i = 0; i < enteredCode.length(); i++) lcd.print("_"); // Display underscores
return;
} else if (key == 'A') { // If key is A
if (enteredCode == correctCode) { // Check if code is correct
armSystem(); // Arm the system
allowPasswordChange = false; // Disable password change
} else {
incorrectAttempts++; // Increment incorrect attempt counter
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Incorrect Code"); // Display incorrect code message
delay(2000); // Wait 2 seconds
if (incorrectAttempts >= 3) { // Check if attempts exceed limit
triggerAlarm(); // Trigger alarm system
allowPasswordChange = false; // Disable password change
}
enteredCode = ""; // Reset entered code
}
} else if (key == 'D' && isArmed) { // If key is D and system is armed
if (enteredCode == correctCode) { // Check if code is correct
disarmSystem(); // Disarm the system
allowPasswordChange = true; // Allow password change
} else {
incorrectAttempts++; // Increment incorrect attempt counter
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Incorrect Code"); // Display incorrect code message
delay(2000); // Wait 2 seconds
if (incorrectAttempts >= 3) { // Check if attempts exceed limit
triggerAlarm(); // Trigger alarm system
allowPasswordChange = false; // Disable password change
}
enteredCode = ""; // Reset entered code
}
} else if (key == 'C' && allowPasswordChange && !isArmed) { // If key is C
if (enteredCode == correctCode) { // Check if code is correct
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Enter New Code:"); // Prompt for new code
enteredCode = ""; // Reset entered code
delay(2000); // Wait 2 seconds
while (true) {
keyIndex = readKeypad(); // Read keypad input
if (keyIndex != -1) {
key = keyMap[keyIndex]; // Get the pressed key
if (isdigit(key) && key != lastKeyPressed && enteredCode.indexOf(key) == -1) { // Prevent repeats
enteredCode += key; // Append key to entered code
lastKeyPressed = key; // Update last key pressed
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("New Code: "); // Display "New Code:" message
for (int i = 0; i < enteredCode.length(); i++) lcd.print("_"); // Display underscores
} else if (key == 'A') { // If key is A
correctCode = enteredCode; // Set new correct code
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Password Changed"); // Display password changed message
delay(2000); // Wait 2 seconds
enteredCode = ""; // Reset entered code
break;
}
}
}
} else {
enteredCode = ""; // Clear entered code
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Code Cleared"); // Display code cleared message
delay(1000); // Wait 1 second
}
}
}
} else {
keyPreviouslyPressed = false; // Reset key press status
}
// Check sensor states
if (isArmed) {
if (digitalRead(button1Pin) == HIGH || digitalRead(button2Pin) == HIGH) { // Check sensors
triggerAlarm(); // Trigger alarm if sensor active
}
} else {
// Display sensor status in disarm state
if (enteredCode.length() == 0 && !alarmActive) {
if (digitalRead(button1Pin) == HIGH) {
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Check Windows "); // Display check windows message
lcd.setCursor(0, 1); // Move cursor to next line
lcd.print("Check Sensors "); // Display check sensors message
} else if (digitalRead(button2Pin) == HIGH) {
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Motion Detected "); // Display motion detected message
lcd.setCursor(0, 1); // Move cursor to next line
lcd.print("Door Open "); // Display door open message
} else {
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("DISARMED "); // Display disarmed status
}
}
}
// Non-blocking alarm handling
if (alarmActive) {
unsigned long currentMillis = millis(); // Get current time
if (currentMillis - alarmStartTime < 30000) { // Check alarm duration
if ((currentMillis / 500) % 2 == 0) { // Blink buzzer and LED
digitalWrite(buzzerPin, HIGH); // Turn buzzer on
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(buzzerPin, LOW); // Turn buzzer off
digitalWrite(ledPin, LOW); // Turn LED off
}
} else {
alarmActive = false; // Deactivate alarm
digitalWrite(buzzerPin, LOW); // Turn buzzer off
digitalWrite(ledPin, LOW); // Turn LED off
}
}
}
void armSystem() {
isArmed = true; // Set system armed status
enteredCode = ""; // Clear entered code after arming
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("ARMED"); // Display armed status
delay(2000); // Wait 2 seconds
return;
}
void disarmSystem() {
isArmed = false; // Set system disarmed status
lcd.display(); // Display LCD screen
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("DISARMED"); // Display disarmed status
digitalWrite(buzzerPin, LOW); // Turn buzzer off
digitalWrite(ledPin, LOW); // Turn LED off
delay(2000); // Wait 2 seconds
enteredCode = ""; // Clear entered code
return;
}
void triggerAlarm() {
lcd.display(); // Display LCD screen
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to start position
lcd.print("Report"); // Display report message
alarmStartTime = millis(); // Record alarm start time
alarmActive = true; // Set alarm status active
}
Editor is loading...
Leave a Comment