Trying to build a Door and Sound sensors



  • Hello,
    I'm trying to build a door and sound sensors with the same arduino.
    But I have some issue.
    Code seems fine but when the door is open status is changing every time from 1 to 0 to 1 to 0 ...
    But when door is closed it's not happening.

    Can you help me with this ?

    Here is my code: (I've commented the gw.send so gateway is not flood)

    // Sound sensor come fome:
    // From Henry's Bench
    // http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-sound-detection-sensor-tutorial-and-user-manual/
    // Arduino Sound Detection Sensor Module
    
    // Door sensor come from:
    // 
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #define ID 100
    #define CHILD_ID 101
    #define OPEN 1
    #define CLOSE 0
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    #define SOUND_PIN 5  // Arduino Digital I/O pin for sound sensor
    
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    MyMessage msgring(ID, V_TRIPPED);
    MyMessage msgdoor(CHILD_ID,V_TRIPPED);
    
    int soundDetectedVal = HIGH; // This is where we record our Sound Measurement
    boolean bAlarm = false;
    unsigned long lastSoundDetectTime; // Record the time that we measured a sound
    int soundAlarmTime = 500; // Number of milli seconds to keep the sound alarm high
    
    void setup ()
    {
      Serial.begin(115200);
      gw.begin();
      pinMode(SOUND_PIN,INPUT) ; // input from the Sound Detection Module
      pinMode(BUTTON_PIN,INPUT);
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      gw.present(ID, S_DOOR);
      gw.present(CHILD_ID, S_DOOR);
    }
    void loop ()
    {
      soundDetectedVal = digitalRead (SOUND_PIN) ; // read the sound alarm time
      debouncer.update();
      int value = debouncer.read();
      if (value != oldValue) {
         Serial.print("Door value: ");
         Serial.println(value);
         //gw.send(msgdoor.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      if (soundDetectedVal == LOW) // If we hear a sound
      {
        lastSoundDetectTime = millis(); // record the time of the sound alarm
        // The following is so you don't scroll on the output screen
        if (!bAlarm){
          Serial.println("LOUD, LOUD");
          //gw.send(msgring.set(OPEN)); 
          bAlarm = true;
        }
      }
      else
      {
        if( (millis()-lastSoundDetectTime) > soundAlarmTime  &&  bAlarm){
          Serial.println("quiet");
          //gw.send(msgring.set(CLOSE)); 
          bAlarm = false;
        }
      }
    }
    

    Sound and soor sensor are connected to the same ground.

    Here is the output:

    send: 18-18-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 18-18-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
    send: 18-18-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-18 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    sensor started, id=18, parent=0, distance=1
    send: 18-18-0-0 s=100,c=0,t=0,pt=0,l=0,sg=0,st=ok:
    send: 18-18-0-0 s=101,c=0,t=0,pt=0,l=0,sg=0,st=ok:
    

    Door is lock:

    Door value: 0
    

    Testing sound sensor, working fine:

    LOUD, LOUD
    quiet
    LOUD, LOUD
    quiet
    

    Door is open it's flapping:

    Door value: 1
    Door value: 0
    Door value: 1
    Door value: 0
    Door value: 1
    Door value: 0
    Door value: 1
    

    Thank you !


  • Contest Winner

    @chatainsim 5ms for debouncer might be a bit low. I use 50ms without any problems

    Have you added a pullup resistor to the door sensor? If not try changing this in your sketch:

    //  pinMode(BUTTON_PIN,INPUT);
     pinMode(BUTTON_PIN,INPUT_PULLUP);
      debouncer.attach(BUTTON_PIN);
    


  • Thanks @TheoL , I've tried with 10ms but the door sensor doesn't detect the opening.
    No, I haven't added a pullup resistor to the door sensor.
    I'll try your change.



  • Thank you @TheoL !
    It's working fine with your change.


  • Contest Winner

    @chatainsim Glad I could help.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts