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. General Discussion
  3. Buzzer when door opened longer than X seconds

Buzzer when door opened longer than X seconds

Scheduled Pinned Locked Moved General Discussion
3 Posts 2 Posters 1.6k Views 3 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.
  • ChakkieC Offline
    ChakkieC Offline
    Chakkie
    wrote on last edited by Chakkie
    #1

    Hi All,

    I have got the buzzer module today and I want to use it on my exist frigde door contact sensor. What I want to do is when one of the fridge door is open more than 30 seconds the buzzer will turn on. And when door is closed the buzz stops.

    The full code is here below:

    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_Koel 31
    #define CHILD_ID_Vries 32
    #define BUTTON_PIN_Koel  7  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_Vries  8  // Arduino Digital I/O pin for button/reed switch
    #define INTERRUPT BUTTON_PIN_Koel -2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define INTERRUPT BUTTON_PIN_Vries -2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define buzzPin 2 // I/O-pin from buzzer connects here
    
    unsigned long currentMillis = millis();
    
    MySensor gw;
    Bounce debouncerKoel = Bounce();
    Bounce debouncerVries = Bounce();
    int oldValueKoel = -1;
    int oldValueVries = -1;
    int buzz = 0;
    
    unsigned long DoorDelay = 30000;
    unsigned long DoorMillis;
    
    MyMessage msgKoel(CHILD_ID_Koel, V_TRIPPED);
    MyMessage msgVries(CHILD_ID_Vries, V_TRIPPED);
    
    void setup()
    {
      gw.begin(NULL, 204, false, 0);
    
      // Setup the button
      pinMode(BUTTON_PIN_Koel, INPUT);
      pinMode(BUTTON_PIN_Vries, INPUT);
    
      //Buzz pin
      pinMode(buzzPin, OUTPUT);
      digitalWrite(buzzPin, HIGH);
    
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_Koel, HIGH);
      digitalWrite(BUTTON_PIN_Vries, HIGH);
      // After setting up the button, setup debouncer
      debouncerKoel.attach(BUTTON_PIN_Koel);
      debouncerVries.attach(BUTTON_PIN_Vries);
      debouncerKoel.interval(5);
      debouncerVries.interval(5);
    
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      gw.present(CHILD_ID_Koel, S_DOOR);
      gw.present(CHILD_ID_Vries, S_DOOR);
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop()
    {
      debouncerKoel.update();
      // Get the update value
      int valueKoel = debouncerKoel.read();
    
      if (valueKoel != oldValueKoel) {
        // Send in the new value
        gw.send(msgKoel.set(valueKoel == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
        oldValueKoel = valueKoel;
    
      }
    
    
    
      debouncerVries.update();
      // Get the update value
      int valueVries = debouncerVries.read();
    
      if (valueVries != oldValueVries) {
        // Send in the new value
        gw.send(msgVries.set(valueVries == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
        oldValueVries = valueVries;
        buzz = digitalRead(BUTTON_PIN_Vries);
        Serial.println(buzz);
    
        if (buzz == HIGH) {
          digitalWrite(buzzPin, buzz); // Tone OFF
          Serial.println("Buzzer off");
        }
        if (buzz == LOW) {
          DoorMillis = millis();
          Serial.println("Counter start");
          Serial.println(DoorMillis);
        }
    
        if ((millis() - DoorMillis) > DoorDelay) {
          digitalWrite(buzzPin, buzz); // Tone ON
          Serial.println("Buzzer triggerd");
    
        }
    
      }
    
      // Sleep until interrupt comes in on motion sensor. Send update every two minute.
      //  gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
      //  Serial.println("sleep");
    }
    

    Below is part of the code where I tired to get the code work. I think I am doing something wrong.

    debouncerVries.update();
      // Get the update value
      int valueVries = debouncerVries.read();
    
      if (valueVries != oldValueVries) {
        // Send in the new value
        gw.send(msgVries.set(valueVries == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
        oldValueVries = valueVries;
        buzz = digitalRead(BUTTON_PIN_Vries);
        Serial.println(buzz);
    
        if (buzz == HIGH) {
          digitalWrite(buzzPin, buzz); // Tone OFF
          Serial.println("Buzzer off");
        }
        if (buzz == LOW) {
          DoorMillis = millis();
          Serial.println("Counter start");
          Serial.println(DoorMillis);
        }
    
        if ((millis() - DoorMillis) > DoorDelay) {
          digitalWrite(buzzPin, buzz); // Tone ON
          Serial.println("Buzzer triggerd");
    
        }
    

    Does anyone know how the code should look like

    Thanks

    Raspberry Pi 2
    Domoticz
    RFXCOM
    ZWAVE Aeon stick
    Coming soon Arduino mysensors GW

    1 Reply Last reply
    0
    • Boots33B Offline
      Boots33B Offline
      Boots33
      Hero Member
      wrote on last edited by Boots33
      #2

      Hi

      I think your problem is where you have the code

      if ((millis() - DoorMillis) > DoorDelay) {
            digitalWrite(buzzPin, buzz); // Tone ON
            Serial.println("Buzzer triggerd");
      
          }
      
      

      you have it nested inside the valueVries if statement which means it will only excecute when a change of state is detected. This means when the switch is first activated the timeout will not have expired and then it will not be retested until the switch state has changed, which will then have turned off.

      you need to move that code to the main loop so it is continually tested and try something like

      if ((millis() - DoorMillis) > DoorDelay && buzz == LOW) {
            digitalWrite(buzzPin, buzz); // Tone ON
            Serial.println("Buzzer triggerd");
      }
      
      ChakkieC 1 Reply Last reply
      0
      • Boots33B Boots33

        Hi

        I think your problem is where you have the code

        if ((millis() - DoorMillis) > DoorDelay) {
              digitalWrite(buzzPin, buzz); // Tone ON
              Serial.println("Buzzer triggerd");
        
            }
        
        

        you have it nested inside the valueVries if statement which means it will only excecute when a change of state is detected. This means when the switch is first activated the timeout will not have expired and then it will not be retested until the switch state has changed, which will then have turned off.

        you need to move that code to the main loop so it is continually tested and try something like

        if ((millis() - DoorMillis) > DoorDelay && buzz == LOW) {
              digitalWrite(buzzPin, buzz); // Tone ON
              Serial.println("Buzzer triggerd");
        }
        
        ChakkieC Offline
        ChakkieC Offline
        Chakkie
        wrote on last edited by Chakkie
        #3

        @Boots33 Thanks for the help. The main problem was how to set the time when the doors are opened. I have tried several attempts and everytime the timer is reset. Therefore the buzzer will never be triggered because the timer and the current time is always the same.

        I have solved this problem. The working script is here below:

        
        #include <MySensor.h>
        #include <SPI.h>
        #include <Bounce2.h>
        
        #define CHILD_ID_Koel 31
        #define CHILD_ID_Vries 32
        #define BUTTON_PIN_Koel  7  // Arduino Digital I/O pin for button/reed switch
        #define BUTTON_PIN_Vries  8  // Arduino Digital I/O pin for button/reed switch
        #define INTERRUPT BUTTON_PIN_Koel -2 // Usually the interrupt = pin -2 (on uno/nano anyway)
        #define INTERRUPT BUTTON_PIN_Vries -2 // Usually the interrupt = pin -2 (on uno/nano anyway)
        #define buzzPin 2 // I/O-pin from buzzer connects here
        
        unsigned long currentMillisKoel;
        unsigned long currentMillisVries;
        
        MySensor gw;
        Bounce debouncerKoel = Bounce();
        Bounce debouncerVries = Bounce();
        int oldValueKoel = -1;
        int oldValueVries = -1;
        int KoelBuzz = 0;
        int VriesBuzz = 0;
        
        unsigned long DoorDelay = 5000UL;
        unsigned long KoelTimer = 0UL;
        unsigned long VriesTimer = 0UL;
        
        MyMessage msgKoel(CHILD_ID_Koel, V_TRIPPED);
        MyMessage msgVries(CHILD_ID_Vries, V_TRIPPED);
        
        void setup()
        {
          gw.begin(NULL, 204, false, 0);
        
          // Setup the button
          pinMode(BUTTON_PIN_Koel, INPUT);
          pinMode(BUTTON_PIN_Vries, INPUT);
        
          //Buzz pin
          pinMode(buzzPin, OUTPUT);
          digitalWrite(buzzPin, HIGH);
        
          // Activate internal pull-up
          digitalWrite(BUTTON_PIN_Koel, HIGH);
          digitalWrite(BUTTON_PIN_Vries, HIGH);
          // After setting up the button, setup debouncer
          debouncerKoel.attach(BUTTON_PIN_Koel);
          debouncerVries.attach(BUTTON_PIN_Vries);
          debouncerKoel.interval(5);
          debouncerVries.interval(5);
        
          // Register binary input sensor to gw (they will be created as child devices)
          // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
          // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
          gw.present(CHILD_ID_Koel, S_DOOR);
          gw.present(CHILD_ID_Vries, S_DOOR);
        }
        
        
        //  Check if digital input has changed and send in new value
        void loop()
        {
        
          // Fridge door script
          debouncerKoel.update();
          // Get the update value
          int valueKoel = debouncerKoel.read();
        
          if (valueKoel != oldValueKoel) {
            // Send in the new value
            gw.send(msgKoel.set(valueKoel == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
            oldValueKoel = valueKoel;
            KoelBuzz = digitalRead(BUTTON_PIN_Koel);
            Serial.println(KoelBuzz);
            if (KoelBuzz == LOW) { //Door is open.
              KoelTimer = millis(); //  Set the time. 
              Serial.println(KoelTimer);
            }
          }
        
          // check how long the doors are opened
          unsigned long currentMillisKoel = millis();
          if ((currentMillisKoel - KoelTimer) >= DoorDelay && KoelBuzz == LOW) {
        //    Serial.println("Counter start");
        //    Serial.println(KoelTimer);
        //    Serial.println(currentMillisKoel);
            digitalWrite(buzzPin, KoelBuzz); // Tone ON
        //    Serial.println("Buzzer triggerd");
        
          }
        
        
        // Freezer door script
          debouncerVries.update();
          // Get the update value
          int valueVries = debouncerVries.read();
        
        
          if (valueVries != oldValueVries) {
            // Send in the new value
            gw.send(msgVries.set(valueVries == HIGH ? 0 : 1)); // change this value to if NC then 0 : 1; if NO then 1 : 0
            oldValueVries = valueVries;
            VriesBuzz = digitalRead(BUTTON_PIN_Vries);
            Serial.println(VriesBuzz);
            if (VriesBuzz == LOW) { //Door is open.
              VriesTimer = millis(); //  Set the time. 
              Serial.println(VriesTimer);
            }
          }
          if (VriesBuzz && KoelBuzz == HIGH) {
            digitalWrite(buzzPin, VriesBuzz); // Tone OFF
          }
          
          // check how long the doors are opened
          unsigned long currentMillisVries = millis();
          if ((currentMillisVries - VriesTimer) >= DoorDelay && VriesBuzz == LOW) {
        //    Serial.println("Counter start");
        //    Serial.println(VriesTimer);
        //    Serial.println(currentMillisVries);
            digitalWrite(buzzPin, VriesBuzz); // Tone ON
        //    Serial.println("Buzzer triggerd");
        
          }
        
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        //  gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
        //  Serial.println("sleep");
        }
        

        Raspberry Pi 2
        Domoticz
        RFXCOM
        ZWAVE Aeon stick
        Coming soon Arduino mysensors GW

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


        19

        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