Arduino Fire/Security System
An Arduino Fire/Security System with multiple sensors and keypad for intruder/fire detection.
Devices & Components
1
Arduino Uno Rev3
Software & Tools
Arduino IDE
Project description
Code
cs_220_capstone_project_arduino_fire_security_system1
cpp
1// Include appropriate libraries 2#include <LiquidCrystal.h> 3#include <Keypad.h> 4// LCD Constants 5const int RS = 2; 6const int E = 3; 7const int DB4 = 4; 8const int DB5 = 5; 9const int DB6 = 6; 10const int DB7 = 7; 11 12//Fire/Security component constants 13const int BUZZER = A0; 14const int SMOKE = A1; 15const int BUTTON = A2; 16const int HEAT = A3; 17const int MOTION = A4; 18// Temp sensor constant 19const char DEGREE = (char) 178; 20 21// Keypad constants 22const byte ROWS = 3; 23const byte COLS = 3; 24 25const char KEYMAP[ROWS][COLS] = 26{ 27{'1', '2', '3'}, 28{'4', '5', '6'}, 29{'7', '8', '9'}, 30}; 31byte ROW_PINS[ROWS] = {13, 12, 11}; // Rows 0 to 2 on keypad 32byte COL_PINS[COLS] = {10, 9, 8}; // Columns 0 to 2 on keypad 33// Disarm code 34const String CODE = "1234"; 35// Variable to store key press 36char key; 37 38// Instantiate a Keypad object 39Keypad keypad = Keypad(makeKeymap(KEYMAP), ROW_PINS, COL_PINS, ROWS, COLS); 40// String to store keypad input 41String input = ""; 42// Instantiate LCD object 43LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 44 45// Smoke sensor reading 46int gasValue = 0; 47// Heat detector readings 48int tempValue; 49int tempC; 50// PIR sensor reading 51int pirState; 52// Manual activation reading 53int buttonPress; 54// Alarm type variable 55int alarmType; 56// Variable to determine if alarm is latched 57bool alarmLatched = false; 58// Variables for Code-3 fire tone 59unsigned long previousMillis = 0; 60int code3Step = 0; 61void setup() 62{ 63 // Initialize LCD and Cursor 64 lcd.begin(16, 2); 65 lcd.setCursor(0, 0); 66 // Input and Output Pins 67 pinMode(HEAT, INPUT); 68 pinMode(BUZZER, OUTPUT); 69 pinMode(SMOKE, INPUT); 70 pinMode(MOTION, INPUT); 71 pinMode(BUTTON, INPUT); 72 73} 74 75void playCode3() { 76 unsigned long currentMillis = millis(); // Keep track of ms the program has run for 77 78 int durations[] = {500, 500, 500, 500, 500, 1500}; 79 // ON, OFF, ON, OFF, ON, OFF(long) 80 // Run the sequence above 81 if (currentMillis - previousMillis >= durations[code3Step]) { 82 previousMillis = currentMillis; 83 84 code3Step++; 85 // Restart the sequence 86 if (code3Step >= 6) { 87 code3Step = 0; 88 } 89 90 // Even steps = ON, Odd = OFF 91 if (code3Step % 2 == 0) { 92 // 520 Hz Low Frequency fire alarm tone 93 tone(BUZZER, 520); 94 } else { 95 noTone(BUZZER); 96 } 97 } 98} 99 100void loop() 101{ 102 // Read sensor values 103 pirState = digitalRead(MOTION); 104 gasValue = analogRead(SMOKE); 105 buttonPress = digitalRead(BUTTON); 106 tempValue = analogRead(HEAT); 107 // Map temp value to °C 108 tempC = map(tempValue, 20, 358, -40, 125); 109 110 // Latch alarm if not latched, then determine alarm type (security or fire) 111 if (!alarmLatched) { 112 if (pirState == HIGH) { 113 alarmLatched = true; 114 alarmType = 1; // security 115 } 116 else if (gasValue > 550) { 117 alarmLatched = true; 118 alarmType = 2; // fire 119 } 120 else if (buttonPress == HIGH) { 121 alarmLatched = true; 122 alarmType = 2; // fire 123 } 124 else if (tempC > 57) { 125 alarmLatched = true; 126 alarmType = 2; // fire 127 } 128} 129 130 if (alarmLatched) { 131 if (alarmType == 1) { 132 // SECURITY (constant tone) 133 tone(BUZZER, 800); 134 } 135 else { 136 // FIRE (Code-3) 137 playCode3(); 138 } 139 // Print the appropriate alarm type to the LCD 140 if (pirState == HIGH) { 141 lcd.setCursor(0,0); 142 lcd.print("SECURITY ALARM"); 143 } 144 else if (gasValue > 550) { 145 lcd.setCursor(0,0); 146 lcd.print("FIRE (SMOKE) "); 147 } 148 else if (buttonPress == HIGH){ 149 lcd.setCursor(0,0); 150 lcd.print("FIRE (PULL) "); 151 } 152 else if (tempC > 57) { 153 lcd.setCursor(0,0); 154 lcd.print("FIRE (HEAT) "); 155 } 156 157 // Setup the LCD to enter code to disarm fire/security alarms 158 lcd.setCursor(0,1); 159 lcd.print("ENTER CODE:"); 160 key = keypad.getKey(); 161 // Print the entered keypad keys on the LCD 162 if (key) { 163 input += key; 164 lcd.setCursor(11,1); 165 lcd.print(input); 166 } 167 // If 4 digit code entered, determine if it is correct 168 if (input.length() == CODE.length()) { 169 // If the code is correct, turn off buzzer and reset system 170 if (input == CODE) { 171 alarmLatched = false; 172 alarmType = 0; 173 noTone(BUZZER); 174 lcd.clear(); 175 lcd.setCursor(0,0); 176 lcd.print("SYSTEM RESET"); 177 delay(1000); 178 lcd.clear(); 179 } 180 // Else, clear out the code from the LCD and do not reset system 181 else { 182 lcd.setCursor(11,1); 183 lcd.print(" "); 184 delay(1000); 185 } 186 // After a code is entered, clear out the code from the LCD 187 input = ""; 188 lcd.setCursor(11,1); 189 } 190 } 191 // If no events, system remains normal 192 else { 193 lcd.setCursor(0,0); 194 lcd.print("SYSTEM NORMAL"); 195 } 196 }
Comments
Only logged in users can leave comments