Devices & Components
Arduino Uno Rev3
Breadboard (generic)
LEDs
Resistor 330 ohm
Buzzer
PIR Motion Sensor (generic)
Project description
Code
Motion Sensing Alarm with Keypad & Password
arduino
1#include <Keypad.h> // library for keyboard 2#include <Password.h> // library for password 3 4Password password = Password( "1234" ); // password 5 6const byte rows = 4; // four rows 7const byte cols = 4; // three columns 8char keys[rows][cols] = { // keys on keypad 9 10{'1','2','3','A'}, 11{'4','5','6','B'}, 12{'7','8','9','C'}, 13{'*','0','#','D'}, 14 15}; 16 17byte rowPins[rows] = {9,8,7,6}; 18byte colPins[cols] = {5,4,3,2}; 19Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); 20 21 22 23#define sensorz A3 // pin for PIR sensor data 24//#define contact 0 // pin for on/off alarm 25#define alrm 12 // pin for siren, buzzer 26#define redLed A2 // pin for red led 27#define greenLed A0 // pin for green led 28#define yellowLed A1 // pin for blue led 29int contact = 10; //used to immediately on/off alarm 30int val; 31int ledBlink; 32 33int sensorzData; 34unsigned long ceas, timpmemorat; 35 36int intarziereactivare = 20; // To delay for standby to armed 37int intarzieredezactivare = 10; // To delay for triggered to alarm activated 38int timpurlat = 10; // Time of alarm is on 39 40// This is the variable for states "0" 41char caz = 0; 42 43int sistem = 0; // system is 0 for off and 1 for on 44 45 46/* 47States for 48 49 0. - off 50 1. - stand-by 51 2. - waitting 52 3. - countdown 53 4. - alarm 54 55*/ 56 57void setup() 58 { 59 keypad.addEventListener(keypadEvent); // an object is created for tracking keystrokes 60 61 Serial.begin(9600); //Used for troubleshooting 62 pinMode(alrm, OUTPUT); 63 pinMode(sensorz, INPUT); 64 pinMode(contact, INPUT); 65 pinMode(redLed, OUTPUT); 66 pinMode(yellowLed, OUTPUT); 67 pinMode(greenLed, OUTPUT); 68 digitalRead(contact); 69 Serial.println("System startup"); //Used for troubleshooting 70 Serial.println("Alarm button status:"); //used for troubleshooting 71 Serial.println(digitalRead(contact)); //used for troubleshooting 72 } 73 74void loop() 75 76 { 77 78 ceas = millis(); // read the internal clock 79 val = digitalRead(contact); 80 81keypad.getKey(); 82 83 84 if (sistem%2 == 0) 85 { 86 // alarm is off 87 digitalWrite(greenLed, LOW); 88 digitalWrite(redLed, LOW); 89 digitalWrite(yellowLed, HIGH); 90 //Serial.println(contact); //Used for troubleshooting 91 92 digitalWrite(alrm, LOW); 93 caz = 0; 94 // Serial.println("System is OFF !"); // Used for troubleshooting 95 96 } 97 98 else 99 { 100 // alarm is on 101 if(caz == 0) 102 { 103 caz = 1; 104 timpmemorat = ceas; 105 digitalWrite(yellowLed, HIGH); 106 } 107 108 if(caz == 1) // system waiting 109 { 110 111 if ((ceas%1000)<500) digitalWrite(greenLed, HIGH); 112 else digitalWrite(greenLed, LOW); 113 keypad.getKey(); 114 if(ceas >= timpmemorat + intarziereactivare * 1000) {caz = 2;} 115 //Serial.println("System is arming !"); // Used for troubleshooting 116 } 117 118 if(caz == 2) // system is armed 119 { 120 digitalWrite(greenLed, HIGH); 121 keypad.getKey(); 122 123 sensorzData = digitalRead(sensorz); 124 //Serial.print("sensorzdData = "); //Used for troubleshooting 125 //Serial.println(sensorzData); //Used for troubleshooting 126 127 // if(sensorzData > 600) {caz = 3; timpmemorat = ceas;} 128 if(sensorzData == HIGH) 129 { 130 caz = 3; 131 timpmemorat = ceas; 132 digitalWrite(greenLed, LOW); 133 } 134 Serial.println("System is armed !"); // Used for Troubleshooting 135 } 136 137 if(caz == 3) // system is triggered and countdown 138 { 139 140 if ((ceas%500)<100) digitalWrite(redLed, HIGH); 141 else digitalWrite(redLed, LOW); 142 keypad.getKey(); 143 if(ceas >= timpmemorat + intarzieredezactivare * 10) {caz = 4; timpmemorat = ceas;} 144 Serial.println("System is triggered and is countdown !"); //Used for troubleshooting 145 } 146 147 if(caz == 4) // siren (buzzer) is active 148 { 149 //digitalWrite(alrm, HIGH); 150 digitalWrite(redLed, HIGH); 151 Serial.println("Siren is active !"); //Used for troubleshooting 152 153 154// For siren 155 156 //tone( 10, 10000, 100); // Simple Alarm Tone 157 for(double x = 0; x < 0.92; x += 0.01){ // Elegant Alarm Tone 158 tone(10, sinh(x+8.294), 10); 159 delay(1); 160 } 161 162 163 keypad.getKey(); 164 if(ceas >= timpmemorat + timpurlat * 1000) {caz = 2; digitalWrite(alrm, LOW); digitalWrite(redLed, LOW);} 165 } 166 } 167 } 168 169 //take care of some special events 170void keypadEvent(KeypadEvent eKey){ 171 switch (keypad.getState()){ 172 case PRESSED: 173 Serial.print("Pressed: "); 174 Serial.println(eKey); 175 switch (eKey){ 176 case '*': checkPassword(); break; 177 case '#': password.reset(); break; 178 default: password.append(eKey); 179 } 180 } 181} 182 183 184 void checkPassword(){ 185 if (password.evaluate()){ 186 Serial.println("Success"); //Used for troubleshooting 187 sistem++; 188 password.reset(); 189 Serial.println("Disarmed");//Add code to run if it works 190 }else{ 191 Serial.println("Wrong"); //Used for troubleshooting 192 //add code to run if it did not work 193 ledBlink = 0; 194 while (ledBlink <= 5){ 195 digitalWrite(redLed, HIGH); 196 delay(100); 197 digitalWrite(redLed, LOW); 198 delay(100); 199 ledBlink++; 200 } 201 password.reset(); 202 } 203}
Downloadable files
3D Printed Box 08
3D Printed Box 08
3D Printed Box 06
3D Printed Box 06
3D Printed Box 07
3D Printed Box 07
3D Printed Box 04
3D Printed Box 04
3D Printed Box 08
3D Printed Box 08
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password

3D Printed Box 10
3D Printed Box 10
3D Printed Box 04
3D Printed Box 04
3D Printed Box 09
3D Printed Box 09
3D Printed Box 06
3D Printed Box 06
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
3D Printed Box 02
3D Printed Box 02
3D Printed Box 11
3D Printed Box 11
3D Printed Box 05
3D Printed Box 05
3D Printed Box 03
3D Printed Box 03
3D Printed Box 01
3D Printed Box 01
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
3D Printed Box 11
3D Printed Box 11
3D Printed Box 05
3D Printed Box 05
3D Printed Box 03
3D Printed Box 03
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password
Motion Sensing Alarm with Keypad & Password

3D Printed Box 01
3D Printed Box 01
3D Printed Box 09
3D Printed Box 09
3D Printed Box 02
3D Printed Box 02
3D Printed Box 10
3D Printed Box 10
Comments
Only logged in users can leave comments