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. Magnetic Door Switch with Buzzer

Magnetic Door Switch with Buzzer

Scheduled Pinned Locked Moved Troubleshooting
9 Posts 3 Posters 3.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.
  • knipexK Offline
    knipexK Offline
    knipex
    wrote on last edited by
    #1

    Hello everybody,
    I hope someone can help me.
    I have connected a magnetic door switch sensor and a buzzer with Arduino.
    Now I would like that when you open the door switch the buzzer starts until the door switch is closed again.

    I have combined the Door/Window/Button Sketch with my buzzer-Sketch, but unfortunately starts the buzzer when the Arduino starts and when the door switch is closed again.
    I want the buzzer only starts when opening the switches and so long until the switch is closed again and will stop again.

    Please help me, thank you in advance

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     */
    
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID 3
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    
    int buzzPin= 2; // I/O-pin from buzzer connects here 
    
    const int wpm = 20; // Morse speed in WPM
    const int dotL = 1200/wpm; // Calculated dot-length
    const int dashL = 3*dotL; // Dash = 3 x dot
    const int sPause = dotL; // Symbol pause = 1 dot
    const int lPause = dashL; // Letter pause = 3 dots
    const int wPause = 7*dotL; // Word pause = 7 dots
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
    
      pinMode(buzzPin,OUTPUT); // Set buzzer-pin as output
      
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN, HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.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, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
         
      dash();
      dot();
      dash();
      dot();  
      delay(lPause-sPause); // Subtracts pause already taken
      dash();
      dash();
      dot();
      dash();
      delay(wPause-sPause); // Subtracts pause already taken
            
      }
    } 
    
    void dot(){
    digitalWrite(buzzPin, LOW); // Tone ON
    delay(dotL); // Tone length
    digitalWrite(buzzPin, HIGH); // Tone OFF
    delay(sPause); // Symbol pause
    return;
    }
    
    void dash(){
    digitalWrite(buzzPin, LOW); // Tone ON
    delay(dashL); // Tone length
    digitalWrite(buzzPin, HIGH); // Tone OFF
    delay(sPause); // Symbol pause
    return;
    }
    
    1 Reply Last reply
    0
    • mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by mfalkvidd
      #2

      I thnk it would be sufficient to add an if clause around the morse code stuff:

      if(value==HIGH){
        dash();
        dot();
        dash();
        dot();  
        delay(lPause-sPause); // Subtracts pause already taken
        dash();
        dash();
        dot();
        dash();
        delay(wPause-sPause); // Subtracts pause already taken
      }
      
      1 Reply Last reply
      1
      • knipexK Offline
        knipexK Offline
        knipex
        wrote on last edited by
        #3

        Many thanks for your response !
        It works. :)
        But how can I change the code so that the morse code is played until the switch is closed again, because at the moment the morse code is played only once, when the switch is opened.

        1 Reply Last reply
        0
        • knipexK Offline
          knipexK Offline
          knipex
          wrote on last edited by
          #4

          I have tried to solve this with INTERRUPT.
          But that does not work !

          #define INTERRUPT BUTTON_PIN-3
          

          and

          if (value==HIGH) {
                 
            dash();
            dot();
            dash();
            dot();  
            delay(lPause-sPause); // Subtracts pause already taken
            dash();
            dash();
            dot();
            dash();
            delay(wPause-sPause); // Subtracts pause already taken
          
            gw.sleep(INTERRUPT,BUTTON_PIN-3, CHANGE);
          
              }
          
          1 Reply Last reply
          0
          • mfalkviddM Offline
            mfalkviddM Offline
            mfalkvidd
            Mod
            wrote on last edited by
            #5

            Why use gw.sleep at all? You want to Arduino to stay awake so it can continue playing, right?

            Side note: Don't use BUTTON_PIN-3, use digitalPinToInterrupt(BUTTON_PIN). That makes the code easier to follow and avoids the risk of forgetting to update one definition but not the other.

            1 Reply Last reply
            0
            • knipexK Offline
              knipexK Offline
              knipex
              wrote on last edited by
              #6

              you are absolutely right.
              I changed the code as follows:

              with HIGH the morse signal is played only once, therefore changed to LOW !

              // Activate internal pull-up
                
                 // DigitalWrite (BUTTON_PIN, HIGH);
              
                 digitalWrite (BUTTON_PIN, LOW);
              
              
              if (value == HIGH) {
              
                 dash ();
                 dot ();
                 dash ();
                 dot ();
                 delay (lPause-sPause); // Subtracts break already taken
                 dash ();
                 dash ();
                 dot ();
                 dash ();
                 delay (wPause-sPause); // Subtracts break already taken
              
                 attachInterrupt (digitalPinToInterrupt (BUTTON_PIN), buzzer_stop, CHANGE);
                       
                   }
              
              
              void buzzer_stop() {
              digitalWrite(buzzPin, HIGH); // Tone OFF
              return;
              }
              
              void dot(){
              digitalWrite(buzzPin, LOW); // Tone ON
              delay(dotL); // Tone length
              digitalWrite(buzzPin, HIGH); // Tone OFF
              delay(sPause); // Symbol pause
              return;
              }
              
              void dash(){
              digitalWrite(buzzPin, LOW); // Tone ON
              delay(dashL); // Tone length
              digitalWrite(buzzPin, HIGH); // Tone OFF
              delay(sPause); // Symbol pause
              return;
              }
              
              
              
              

              But now works with a small error :
              the buzzer makes mose signal in an endless loop, however it is very quiet even though it actually is very noisy ?!

              1 Reply Last reply
              0
              • knipexK Offline
                knipexK Offline
                knipex
                wrote on last edited by
                #7

                I found the mistake, now it's really loud :)

                Thank you so much.

                ChakkieC mfalkviddM 2 Replies Last reply
                0
                • knipexK knipex

                  I found the mistake, now it's really loud :)

                  Thank you so much.

                  ChakkieC Offline
                  ChakkieC Offline
                  Chakkie
                  wrote on last edited by
                  #8

                  @knipex Hi can you please post your working script?? I want the same functionality too. Thanks

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

                  1 Reply Last reply
                  0
                  • knipexK knipex

                    I found the mistake, now it's really loud :)

                    Thank you so much.

                    mfalkviddM Offline
                    mfalkviddM Offline
                    mfalkvidd
                    Mod
                    wrote on last edited by
                    #9

                    @knipex what was the mistake?

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


                    27

                    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