Components and supplies
3
Resistor 220 ohm
1
Breadboard (generic)
3
5 mm LED: Green
1
Tactile Switch, Top Actuated
1
KY-037 (Digital/Analog Sound sensor)
1
I2C 16x2 Arduino LCD Display Module
1
Resistor 10k ohm
1
Jumper wires (generic)
1
Arduino UNO
Apps and platforms
1
Arduino IDE
Project description
Code
Repository link:
Code:
arduino
1/* Arduino 'Knock Knock' Door Access System 2 3 Components: 4 - Arduino Uno 5 - Digital Sound sensor (KY-037) 6 - Green LED (x3) 7 - 220Ohm resistor (x3) 8 - Push button tactile switch 9 - LCD I2C (20x4) 10 - Breadboard 11 - Some jumper wires 12 13 Libraries: 14 - LiquidCrystal_I2C library 15 16 Created on 17 July 2022 by c010blind3ngineer 17*/ 18 19#include <LiquidCrystal_I2C.h> 20 21int soundDetectedVal; // This is where we record our sound measurement 22unsigned long lastSoundDetectTime; // Record the time that we measured a sound 23int soundAlarmTime = 200; // Number of milli seconds to keep the sound alarm high 24boolean noiseAlarm = false; 25 26const int LED1 = 5; 27const int LED2 = 6; 28const int LED3 = 7; 29const int rstBtn = 4; 30 31// Knocking sequence for the 3 LEDs 32int LED1_seq = 2; // Knock 2 times 33int LED2_seq = 1; // Knock 1 time 34int LED3_seq = 3; // Knock 3 times 35 36boolean f_knocks = false; // first sequence of knocks 37boolean s_knocks = false; // second sequence of knocks 38boolean t_knocks = false; // third sequence of knocks 39 40int t = 0; 41int load = 0; 42int knocks = 0; 43int count = 0; 44 45unsigned long f_knocksTime = 0; 46unsigned long t_knocksTime = 0; 47int durationForKnocks = 4000; // normally the knocking sequence I do lasts about 4 seconds 48 49LiquidCrystal_I2C lcd(0x27, 20, 4); 50 51void setup() 52{ 53 Serial.begin(9600); 54 pinMode(LED1, OUTPUT); 55 pinMode(LED2, OUTPUT); 56 pinMode(LED3, OUTPUT); 57 pinMode(rstBtn, INPUT); 58 lcd.init(); 59 lcd.backlight(); 60} 61 62void loop() { 63 while (knocks < 6) { // Once system got all 6 knocks, it will exit the 'while' loop 64 soundDetectedVal = digitalRead(10); // read the sound value from Digital Pin 10 65 if (soundDetectedVal == HIGH) { 66 67 // This will output the "Knock" message only once even if the signal remains at HIGH. 68 if (!noiseAlarm) { 69 lastSoundDetectTime = millis(); 70 Serial.println("Knock"); 71 knocks++; 72 count++; 73 noiseAlarm = true; 74 } 75 } 76 if ((millis() - lastSoundDetectTime) > soundAlarmTime && noiseAlarm) { 77 noiseAlarm = false; 78 } 79 while (load < 1) { // Load the initialization message once 80 doorKnockInit(); 81 load = 1; 82 } 83 84 // Start to check the knocks that correspond to the LEDs 85 turnLED1_ON(); 86 if (f_knocks) { // If first set of knocks is TRUE, go to LED2 function 87 turnLED2_ON(); 88 if (s_knocks) { // If second set of knocks is TRUE, go to LED3 function 89 turnLED3_ON(); 90 } 91 } 92 } 93 delay(1000); // Give system some time to register all 6 knocks 94 95 while (f_knocks && s_knocks && t_knocks) { // When all three sets of knocks TRUE 96 97 // Check to see if User didn't follow the correct knocking 'rhythm' 98 if (t_knocksTime - f_knocksTime < 2000) { 99 while ( t < 1) { 100 lcd.clear(); 101 lcd.setCursor(0, 1); 102 lcd.print(" Access Denied "); 103 delay(1000); 104 lcd.clear(); 105 lcd.setCursor(0, 0); 106 lcd.print("Press reset button.."); 107 t = 1; 108 } 109 // RESET and try again 110 while (digitalRead(rstBtn) != HIGH) {}; 111 if (digitalRead(rstBtn) == HIGH) { 112 resetting(); 113 } 114 } 115 116 // User follows the correct knocking 'rhythm'... 117 // ...to attain the correct duration while knocking 118 if ((t_knocksTime - f_knocksTime > 2000) && (t_knocksTime - f_knocksTime < durationForKnocks)) { 119 while (t < 1) { 120 lcd.clear(); 121 lcd.setCursor(0, 1); 122 lcd.print(" Knocks accepted! "); 123 lcd.setCursor(0, 2); 124 lcd.print(" Access Granted "); 125 t = 1; 126 } 127 delay(5000); 128 // End of Access, exit the system 129 lcd.clear(); 130 lcd.setCursor(0, 1); 131 lcd.print(" Goodbye! "); 132 delay(1000); 133 lcd.clear(); 134 lcd.noBacklight(); 135 f_knocks = false; 136 s_knocks = false; 137 t_knocks = false; 138 digitalWrite(LED1, LOW); 139 digitalWrite(LED2, LOW); 140 digitalWrite(LED3, LOW); 141 } 142 143 // User took too long to enter the knocking 'rhythm' 144 if (t_knocksTime - f_knocksTime > durationForKnocks) { 145 while (t < 1) { 146 lcd.clear(); 147 lcd.setCursor(0, 1); 148 lcd.print(" Ran out of time! "); 149 delay(1000); 150 lcd.clear(); 151 lcd.setCursor(0, 0); 152 lcd.print("Press reset button.."); 153 t = 1; 154 } 155 // RESET and try again 156 while (digitalRead(rstBtn) != HIGH) {}; 157 if (digitalRead(rstBtn) == HIGH) { 158 resetting(); 159 } 160 } 161 } 162 delay(1000); // let the system register the reset before detecting sound 163} 164 165// First Knocking Sequence 166void turnLED1_ON() { 167 if (count == LED1_seq && f_knocks == false) { 168 Serial.println("LED 1 - ACCEPTED"); 169 digitalWrite(LED1, HIGH); 170 count = 0; 171 f_knocks = true; 172 f_knocksTime = millis(); // capture the time AFTER the 'first knocks' completed 173 } 174} 175 176// Second Knocking Sequence 177void turnLED2_ON() { 178 if (count == LED2_seq && s_knocks == false) { 179 Serial.println("LED 2 - ACCEPTED"); 180 digitalWrite(LED2, HIGH); 181 count = 0; 182 s_knocks = true; 183 } 184} 185 186// Third Knocking Sequence 187void turnLED3_ON() { 188 if (count == LED3_seq && t_knocks == false) { 189 Serial.println("LED 3 - ACCEPTED"); 190 digitalWrite(LED3, HIGH); 191 count = 0; 192 t_knocks = true; 193 t_knocksTime = millis(); // capture the time AFTER the 'third knocks' completed 194 } 195} 196 197void doorKnockInit() { 198 lcd.setCursor(0, 0); 199 lcd.print(" Arduino "); 200 lcd.setCursor(0, 1); 201 lcd.print("'Knock Knock' System"); 202 delay(5000); 203 lcd.clear(); 204 lcd.setCursor(0, 1); 205 lcd.print("Please enter knock.."); 206} 207 208void resetting() { 209 lcd.clear(); 210 lcd.setCursor(0, 1); 211 lcd.print("Please enter knock.."); 212 knocks = 0; 213 t = 0; 214 digitalWrite(LED1, LOW); 215 digitalWrite(LED2, LOW); 216 digitalWrite(LED3, LOW); 217 f_knocks = false; 218 s_knocks = false; 219 t_knocks = false; 220} 221
Repository link:
Downloadable files
Schematic:
Schematic:

Circuit:
Circuit:

Circuit:
Circuit:

Schematic:
Schematic:

Comments
Only logged in users can leave comments