Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
viliamk
Published © GPL3+

The door bell opens an electronic door

Opens the electronic door by a morse signal from the door bell

IntermediateProtip711
The door bell opens an electronic door

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Relay Module (Generic)
×1
small microphone
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Trimmer Potentiometer, 10 kohm
Trimmer Potentiometer, 10 kohm
×1
resistor 2k7
×1

Story

Read more

Schematics

DoorBell Circuit

Code

DoorBell

Arduino
Evaluates sounds and opens an electronic door if the sounds matches our sample
/* Code by Viliam Krunka v1.0
 *  
 *  How it works:
 *  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.
 *  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.
 *  If a pause is long enough, it clears recorded sounds and all the process starts again with a next sound.
 */

#define dataPin A1        //microphone pin
#define relayPin 9        //relay pin

const int pauseLength = 80;    //how long a pause is still a sound {in milliseconds)
const int quietLength = 1000;  //after how long quiet start recording a sound again {in milliseconds) 
const int soundTrigger = 150;    //sensitivity of the microphone
const int Dot = 250;           //how long sound is still a dot

int data;         //data from dataPin
bool start;       //if sound started
bool pause;       //if pause started
long startTime;   //time when sound starts
long pauseTime;   //time when pause starts
long quietTime;   //time when quiet started
char counter;     //counts sounds
char sound[3];    //for recording a sound
char sample[3] = {".-."};  //sample of the sound I expect to open a door
int minimum;
int maximum;

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);
  pinMode(dataPin, INPUT);
  counter = 0;
  start = false;
  pause = false;
  quietTime = millis();
  //Serial.begin(9600);
  //Serial.println("SET DONE. Value of dataPin:");
  //Serial.println(analogRead(dataPin));
}


void loop() {
  if (start) {
    if (pause) {
      if (isPause()) {      //if there is a pause, find out how long it is
        if (millis() - pauseTime > pauseLength) {
          EvaluateSound(pauseTime - startTime);    //sound has finished, go to do a job
          
          //start to listen to another sound
          pause = false;
          start = false;
          quietTime = millis();
        }
      } else {
        pause = false;    
      }
    } else {
      if (isPause()) {
        pauseTime = millis();
        pause = true;
      }
    }
  } else {         
    if (isSound()) {    //start the process when a sound is detected
      startTime = millis();
      start = true;
      pause = false;
      //Serial.println("Start = true");
    } else {
      if (millis() - quietTime > quietLength) {     //if there is a long pause, start to listen from the begining
        counter = 0;
        quietTime = millis();
      }
    }
  }
}


bool isSound() {
  minimum = 1023;
  maximum = 0;

  for (int i=0; i<30; i++) {
    data = analogRead(dataPin);
    if (data > maximum) maximum = data;
    if (data < minimum) minimum = data;
  }
  if (maximum - minimum > soundTrigger) { 
    
    //int triggerValue = maximum - minimum;
    //String text = "triggerValue: ";
    //String total = text + triggerValue;
    //Serial.println(total);
  
    return true;
  }
  return false;
}

bool isPause() {
  return !isSound();
}

void EvaluateSound(int soundTime) {
  if (soundTime < Dot) {
    //Serial.println("DOT");
    //Serial.println(soundTime);
    sound[counter] = '.';
  } else {
    //Serial.println("DASH");
    //Serial.println(soundTime);
    sound[counter] = '-';
  }
  
  //Serial.println(int(counter));
  
  counter++;
  if (counter > 2) {
    if (Match(sound,sample)) OpenDoor();
    counter = 0;
  }
}

bool Match(char a1[3], char a2[3]) {
  bool b = true;
  if (a1[0] != a2[0]) b = false;
  if (a1[1] != a2[1]) b = false;
  if (a1[2] != a2[2]) b = false;
  return b;
}

void OpenDoor() {
  //Serial.println("OPEN THE DOOR");
  digitalWrite(relayPin, LOW);
  delay(2000);
  digitalWrite(relayPin, HIGH);
  //Serial.println("READY");
}

Credits

viliamk
14 projects • 5 followers
Contact

Comments

Please log in or sign up to comment.