Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Trying to build a Door and Sound sensors

Trying to build a Door and Sound sensors

Scheduled Pinned Locked Moved Troubleshooting
5 Posts 2 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • chatainsimC Offline
    chatainsimC Offline
    chatainsim
    wrote on last edited by chatainsim
    #1

    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 !

    TheoLT 1 Reply Last reply
    0
    • chatainsimC chatainsim

      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 !

      TheoLT Offline
      TheoLT Offline
      TheoL
      Contest Winner
      wrote on last edited by TheoL
      #2

      @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);
      
      chatainsimC 2 Replies Last reply
      2
      • TheoLT TheoL

        @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);
        
        chatainsimC Offline
        chatainsimC Offline
        chatainsim
        wrote on last edited by chatainsim
        #3

        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.

        1 Reply Last reply
        0
        • TheoLT TheoL

          @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);
          
          chatainsimC Offline
          chatainsimC Offline
          chatainsim
          wrote on last edited by
          #4

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

          TheoLT 1 Reply Last reply
          1
          • chatainsimC chatainsim

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

            TheoLT Offline
            TheoLT Offline
            TheoL
            Contest Winner
            wrote on last edited by
            #5

            @chatainsim Glad I could help.

            1 Reply Last reply
            1
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            17

            Online

            11.7k

            Users

            11.2k

            Topics

            113.1k

            Posts


            Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • MySensors
            • OpenHardware.io
            • Categories
            • Recent
            • Tags
            • Popular