#include <Keypad.h>
#include <LiquidCrystal.h>
// Pins for the RGB LED and Buzzer
const int PIN_R = 3;
const int PIN_G = 4;
const int PIN_B = 5;
const int BUZZER_PIN = 6;
const int FIRE_SENSOR_PIN = 7; // Fire detection sensor input
// Initialize the LCD with the correct pins
LiquidCrystal lcd(52, 50, 48, 46, 44, 42); // RS=52, E=50, D4=48, D5=46, D6=44, D7=42
// Define the Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the keys on the keypad
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
// Connect keypad rows and columns to these pins
byte rowPins[ROWS] = {37, 39, 41, 43}; // Update these pins as necessary
byte colPins[COLS] = {45, 47, 49}; // Update these pins as necessary
// Create the keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Set the correct password
String correctPassword = "1234";
String enteredPassword = "";
// State variable to keep track of system mode
enum SystemState {READY, FIRE_DETECTION, STANDBY} systemState = READY;
// Function to control the RGB LED
void setRGB(int r, int g, int b) {
analogWrite(PIN_R, r);
analogWrite(PIN_G, g);
analogWrite(PIN_B, b);
}
// Function to control the Buzzer
void beep(int times, int duration = 200) {
for (int i = 0; i < times; i++) {
tone(BUZZER_PIN, 1000, duration);
delay(duration);
noTone(BUZZER_PIN);
delay(duration);
}
}
// Smooth scrolling text function
void scrollTextNonBlocking(String message, unsigned long scrollSpeed = 300) {
static unsigned long lastScrollTime = 0;
static int currentPos = 0;
static String scrollMessage;
if (message != scrollMessage) {
scrollMessage = " " + message; // Add 16 spaces at the start
currentPos = 0;
}
if (millis() - lastScrollTime >= scrollSpeed) {
lastScrollTime = millis();
lcd.setCursor(0, 1); // Set cursor to second line
lcd.print(scrollMessage.substring(currentPos, currentPos + 16)); // Print 16 chars of the message
currentPos++;
if (currentPos >= scrollMessage.length()) {
currentPos = 0;
}
}
}
void setup() {
// Initialize the RGB LED and Buzzer pins
pinMode(PIN_R, OUTPUT);
pinMode(PIN_G, OUTPUT);
pinMode(PIN_B, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(FIRE_SENSOR_PIN, INPUT); // Set fire sensor pin as input
// System Initialization
setRGB(0, 0, 255); // Blue LED for system ready
beep(2); // 2 beeps for initialization
// Initialize the LCD
lcd.begin(16, 2);
lcd.clear();
lcd.print("System Ready");
delay(2000);
// Display the initial message
lcd.clear();
lcd.print("Enter Security");
lcd.setCursor(0, 1);
lcd.print("Code:");
}
void loop() {
char key = keypad.getKey(); // Read the key pressed
if (key) { // If a key is pressed
if (key == '#') { // If the key is the '#' key, check the password
lcd.clear();
if (enteredPassword == correctPassword) {
switch (systemState) {
case READY:
// Prompt user to arm or standby
setRGB(0, 255, 0); // Green LED for success
beep(3); // 3 beeps for correct input
lcd.print("Access Granted");
delay(2000);
lcd.clear();
lcd.print("1: Arm Fire");
lcd.setCursor(0, 1);
lcd.print("2: Standby");
// Wait for the user to select an option
while (true) {
char option = keypad.getKey();
if (option == '1') {
setRGB(255, 0, 0); // Constant Red for Fire Detection Activated
lcd.clear();
lcd.print("Fire Detect Mode");
systemState = FIRE_DETECTION;
break;
} else if (option == '2') {
setRGB(0, 0, 255); // Constant Blue for system standby
lcd.clear();
lcd.print("System Standby");
systemState = STANDBY;
break;
}
}
break;
case FIRE_DETECTION:
case STANDBY:
// Neutralize and return to ready state
setRGB(0, 0, 255); // Blue for system ready
beep(1); // 1 beep for disarming
lcd.clear();
lcd.print("System Disarmed");
systemState = READY;
delay(2000);
lcd.clear();
lcd.print("Enter Security");
lcd.setCursor(0, 1);
lcd.print("Code:");
break;
}
} else {
setRGB(255, 0, 0); // Constant Red LED for failure
tone(BUZZER_PIN, 1000); // Continuous beep for incorrect input
lcd.print("Access Denied");
delay(3000);
noTone(BUZZER_PIN); // Stop the beep
lcd.clear();
lcd.print("Enter Security");
lcd.setCursor(0, 1);
lcd.print("Code:");
}
enteredPassword = ""; // Clear the entered password
} else if (key == '*') { // If the key is the '*' key, clear the current entry
enteredPassword = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second line of the LCD
} else {
// Add the pressed key to the password
enteredPassword += key;
lcd.setCursor(0, 1);
lcd.print(enteredPassword);
}
}
// Handle the states as per logic
switch (systemState) {
case FIRE_DETECTION:
setRGB(255, 0, 0); // Constant Red for fire detection
lcd.setCursor(0, 0);
lcd.print("Fire Detect Mode");
scrollTextNonBlocking("Fire Detect Active");
// Check the fire detection sensor
if (digitalRead(FIRE_SENSOR_PIN) == HIGH) {
tone(BUZZER_PIN, 1000); // Continuous beep if fire is detected
} else {
noTone(BUZZER_PIN); // Stop the beep if no fire is detected
}
break;
case STANDBY:
setRGB(0, 0, 255); // Constant Blue for standby
lcd.setCursor(0, 0);
lcd.print("System Standby");
scrollTextNonBlocking("System in Standby");
break;
case READY:
default:
// No action needed in READY state
break;
}
}