Components and supplies
resistor 2k7
Relay Module (Generic)
Trimmer Potentiometer, 10 kohm
small microphone
General Purpose Transistor NPN
Arduino Nano R3
Apps and platforms
Arduino IDE
Project description
Code
DoorBell
arduino
Evaluates sounds and opens an electronic door if the sounds matches our sample
1/* Code by Viliam Krunka v1.0 2 * 3 * How it works: 4 * Reads 5 the length of a sound and writes it as a dot or a dash into an array. If the array 6 matches the sample, opens the door. 7 * The frequency of a sound is ignored by 8 evaluating pauses between sound amplitudes. Longer pauses are evaluated as real 9 ones. Very short ones are parts of the sound. 10 * If a pause is long enough, 11 it clears recorded sounds and all the process starts again with a next sound. 12 13 */ 14 15#define dataPin A1 //microphone pin 16#define relayPin 9 //relay 17 pin 18 19const int pauseLength = 80; //how long a pause is still a sound {in 20 milliseconds) 21const int quietLength = 1000; //after how long quiet start recording 22 a sound again {in milliseconds) 23const int soundTrigger = 150; //sensitivity 24 of the microphone 25const int Dot = 250; //how long sound is still a 26 dot 27 28int data; //data from dataPin 29bool start; //if sound 30 started 31bool pause; //if pause started 32long startTime; //time when 33 sound starts 34long pauseTime; //time when pause starts 35long quietTime; //time 36 when quiet started 37char counter; //counts sounds 38char sound[3]; //for 39 recording a sound 40char sample[3] = {".-."}; //sample of the sound I expect 41 to open a door 42int minimum; 43int maximum; 44 45void setup() { 46 pinMode(relayPin, 47 OUTPUT); 48 digitalWrite(relayPin, HIGH); 49 pinMode(dataPin, INPUT); 50 counter 51 = 0; 52 start = false; 53 pause = false; 54 quietTime = millis(); 55 //Serial.begin(9600); 56 57 //Serial.println("SET DONE. Value of dataPin:"); 58 //Serial.println(analogRead(dataPin)); 59} 60 61 62void 63 loop() { 64 if (start) { 65 if (pause) { 66 if (isPause()) { //if 67 there is a pause, find out how long it is 68 if (millis() - pauseTime > 69 pauseLength) { 70 EvaluateSound(pauseTime - startTime); //sound has 71 finished, go to do a job 72 73 //start to listen to another 74 sound 75 pause = false; 76 start = false; 77 quietTime 78 = millis(); 79 } 80 } else { 81 pause = false; 82 } 83 84 } else { 85 if (isPause()) { 86 pauseTime = millis(); 87 pause 88 = true; 89 } 90 } 91 } else { 92 if (isSound()) { //start 93 the process when a sound is detected 94 startTime = millis(); 95 start 96 = true; 97 pause = false; 98 //Serial.println("Start = true"); 99 100 } else { 101 if (millis() - quietTime > quietLength) { //if there is 102 a long pause, start to listen from the begining 103 counter = 0; 104 quietTime 105 = millis(); 106 } 107 } 108 } 109} 110 111 112bool isSound() { 113 minimum 114 = 1023; 115 maximum = 0; 116 117 for (int i=0; i<30; i++) { 118 data = analogRead(dataPin); 119 120 if (data > maximum) maximum = data; 121 if (data < minimum) minimum = data; 122 123 } 124 if (maximum - minimum > soundTrigger) { 125 126 //int triggerValue 127 = maximum - minimum; 128 //String text = "triggerValue: "; 129 //String 130 total = text + triggerValue; 131 //Serial.println(total); 132 133 return 134 true; 135 } 136 return false; 137} 138 139bool isPause() { 140 return !isSound(); 141} 142 143void 144 EvaluateSound(int soundTime) { 145 if (soundTime < Dot) { 146 //Serial.println("DOT"); 147 148 //Serial.println(soundTime); 149 sound[counter] = '.'; 150 } else { 151 152 //Serial.println("DASH"); 153 //Serial.println(soundTime); 154 sound[counter] 155 = '-'; 156 } 157 158 //Serial.println(int(counter)); 159 160 counter++; 161 162 if (counter > 2) { 163 if (Match(sound,sample)) OpenDoor(); 164 counter 165 = 0; 166 } 167} 168 169bool Match(char a1[3], char a2[3]) { 170 bool b = true; 171 172 if (a1[0] != a2[0]) b = false; 173 if (a1[1] != a2[1]) b = false; 174 if (a1[2] 175 != a2[2]) b = false; 176 return b; 177} 178 179void OpenDoor() { 180 //Serial.println("OPEN 181 THE DOOR"); 182 digitalWrite(relayPin, LOW); 183 delay(2000); 184 digitalWrite(relayPin, 185 HIGH); 186 //Serial.println("READY"); 187}
DoorBell
arduino
Evaluates sounds and opens an electronic door if the sounds matches our sample
1/* Code by Viliam Krunka v1.0 2 * 3 * How it works: 4 * Reads the length of a sound and writes it as a dot or a dash into an array. If the array matches the sample, opens the door. 5 * The frequency of a sound is ignored by evaluating pauses between sound amplitudes. Longer pauses are evaluated as real ones. Very short ones are parts of the sound. 6 * If a pause is long enough, it clears recorded sounds and all the process starts again with a next sound. 7 */ 8 9#define dataPin A1 //microphone pin 10#define relayPin 9 //relay pin 11 12const int pauseLength = 80; //how long a pause is still a sound {in milliseconds) 13const int quietLength = 1000; //after how long quiet start recording a sound again {in milliseconds) 14const int soundTrigger = 150; //sensitivity of the microphone 15const int Dot = 250; //how long sound is still a dot 16 17int data; //data from dataPin 18bool start; //if sound started 19bool pause; //if pause started 20long startTime; //time when sound starts 21long pauseTime; //time when pause starts 22long quietTime; //time when quiet started 23char counter; //counts sounds 24char sound[3]; //for recording a sound 25char sample[3] = {".-."}; //sample of the sound I expect to open a door 26int minimum; 27int maximum; 28 29void setup() { 30 pinMode(relayPin, OUTPUT); 31 digitalWrite(relayPin, HIGH); 32 pinMode(dataPin, INPUT); 33 counter = 0; 34 start = false; 35 pause = false; 36 quietTime = millis(); 37 //Serial.begin(9600); 38 //Serial.println("SET DONE. Value of dataPin:"); 39 //Serial.println(analogRead(dataPin)); 40} 41 42 43void loop() { 44 if (start) { 45 if (pause) { 46 if (isPause()) { //if there is a pause, find out how long it is 47 if (millis() - pauseTime > pauseLength) { 48 EvaluateSound(pauseTime - startTime); //sound has finished, go to do a job 49 50 //start to listen to another sound 51 pause = false; 52 start = false; 53 quietTime = millis(); 54 } 55 } else { 56 pause = false; 57 } 58 } else { 59 if (isPause()) { 60 pauseTime = millis(); 61 pause = true; 62 } 63 } 64 } else { 65 if (isSound()) { //start the process when a sound is detected 66 startTime = millis(); 67 start = true; 68 pause = false; 69 //Serial.println("Start = true"); 70 } else { 71 if (millis() - quietTime > quietLength) { //if there is a long pause, start to listen from the begining 72 counter = 0; 73 quietTime = millis(); 74 } 75 } 76 } 77} 78 79 80bool isSound() { 81 minimum = 1023; 82 maximum = 0; 83 84 for (int i=0; i<30; i++) { 85 data = analogRead(dataPin); 86 if (data > maximum) maximum = data; 87 if (data < minimum) minimum = data; 88 } 89 if (maximum - minimum > soundTrigger) { 90 91 //int triggerValue = maximum - minimum; 92 //String text = "triggerValue: "; 93 //String total = text + triggerValue; 94 //Serial.println(total); 95 96 return true; 97 } 98 return false; 99} 100 101bool isPause() { 102 return !isSound(); 103} 104 105void EvaluateSound(int soundTime) { 106 if (soundTime < Dot) { 107 //Serial.println("DOT"); 108 //Serial.println(soundTime); 109 sound[counter] = '.'; 110 } else { 111 //Serial.println("DASH"); 112 //Serial.println(soundTime); 113 sound[counter] = '-'; 114 } 115 116 //Serial.println(int(counter)); 117 118 counter++; 119 if (counter > 2) { 120 if (Match(sound,sample)) OpenDoor(); 121 counter = 0; 122 } 123} 124 125bool Match(char a1[3], char a2[3]) { 126 bool b = true; 127 if (a1[0] != a2[0]) b = false; 128 if (a1[1] != a2[1]) b = false; 129 if (a1[2] != a2[2]) b = false; 130 return b; 131} 132 133void OpenDoor() { 134 //Serial.println("OPEN THE DOOR"); 135 digitalWrite(relayPin, LOW); 136 delay(2000); 137 digitalWrite(relayPin, HIGH); 138 //Serial.println("READY"); 139}
Downloadable files
DoorBell Circuit
DoorBell Circuit
DoorBell Circuit
DoorBell Circuit
Comments
Only logged in users can leave comments