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. Motion Sensor debounce

Motion Sensor debounce

Scheduled Pinned Locked Moved Troubleshooting
10 Posts 3 Posters 4.0k 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.
  • alexsh1A Offline
    alexsh1A Offline
    alexsh1
    wrote on last edited by
    #1

    Hello,

    I am not sure how this can be done.
    Basically, I have a Panasonic PIR on Moteino with RFM69W working just fine. The problem is that it is spamming GW with messages.
    I need to introduce a delay - if PIR triggered, no more messages to GW for 30 secs or so. My issue is that the node is sleeping:

    sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME)
    

    and millis() are not working during sleeping.

    My question is this: How do I introduce a delay, say 30 sec, in order not to spam my GW/Controller with "Security Lighting" (this is how it is recognised in Domoticz). Such delay must work with the sleep function above.

    Appreciate if you could share your ideas

    Regards
    Alex

    mfalkviddM 1 Reply Last reply
    0
    • alexsh1A alexsh1

      Hello,

      I am not sure how this can be done.
      Basically, I have a Panasonic PIR on Moteino with RFM69W working just fine. The problem is that it is spamming GW with messages.
      I need to introduce a delay - if PIR triggered, no more messages to GW for 30 secs or so. My issue is that the node is sleeping:

      sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME)
      

      and millis() are not working during sleeping.

      My question is this: How do I introduce a delay, say 30 sec, in order not to spam my GW/Controller with "Security Lighting" (this is how it is recognised in Domoticz). Such delay must work with the sleep function above.

      Appreciate if you could share your ideas

      Regards
      Alex

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

      @alexsh1 how about adding

      sleep(30*1000)
      

      just before

      sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME)
      

      perhaps with an if statement that only invokes sleep(30*1000) if the sensor was triggered.

      An alternative could be to add an if-statement that only sends a message if the value was changed from last time.

      Edit: added *1000 to convert the seconds to milliseconds

      alexsh1A 1 Reply Last reply
      0
      • mfalkviddM mfalkvidd

        @alexsh1 how about adding

        sleep(30*1000)
        

        just before

        sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME)
        

        perhaps with an if statement that only invokes sleep(30*1000) if the sensor was triggered.

        An alternative could be to add an if-statement that only sends a message if the value was changed from last time.

        Edit: added *1000 to convert the seconds to milliseconds

        alexsh1A Offline
        alexsh1A Offline
        alexsh1
        wrote on last edited by alexsh1
        #3

        @mfalkvidd, you mean

        define DEBOUNCE 30000 // 
        ............
        sleep(DEBOUNCE);
        

        This just delays it ANYWAY regardless whether the PIR was triggered or not.

        When it comes to "if", during one trigger, my PIR is changing the status from 0 to 1 and back to 0. Unfortunately, I cannot count millis() when sleeping. How can use count 30 secs from the last even when the PIR was triggered please? Very much appreciate your suggestion.

        mfalkviddM 1 Reply Last reply
        0
        • alexsh1A alexsh1

          @mfalkvidd, you mean

          define DEBOUNCE 30000 // 
          ............
          sleep(DEBOUNCE);
          

          This just delays it ANYWAY regardless whether the PIR was triggered or not.

          When it comes to "if", during one trigger, my PIR is changing the status from 0 to 1 and back to 0. Unfortunately, I cannot count millis() when sleeping. How can use count 30 secs from the last even when the PIR was triggered please? Very much appreciate your suggestion.

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

          @alexsh1 hard to say without knowing what else your node does. If it does nothing, the sleep 30000 will be enough. You could connect a RTC, but that might also affect what else the node does.

          alexsh1A 1 Reply Last reply
          0
          • mfalkviddM mfalkvidd

            @alexsh1 hard to say without knowing what else your node does. If it does nothing, the sleep 30000 will be enough. You could connect a RTC, but that might also affect what else the node does.

            alexsh1A Offline
            alexsh1A Offline
            alexsh1
            wrote on last edited by
            #5

            @mfalkvidd The node is a PIR + reporting baro+temp+hum (BME280) every 5 minutes as well as battery voltage.

            mfalkviddM 1 Reply Last reply
            0
            • alexsh1A alexsh1

              @mfalkvidd The node is a PIR + reporting baro+temp+hum (BME280) every 5 minutes as well as battery voltage.

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

              @alexsh1 save the return value from your existing sleep into a variable. Add an if statement that does sleep(30000) only if your node was waken up by interrupt.

              Documentation for the return value is available at https://www.mysensors.org/download/sensor_api_20#sleeping

              alexsh1A 1 Reply Last reply
              0
              • mfalkviddM mfalkvidd

                @alexsh1 save the return value from your existing sleep into a variable. Add an if statement that does sleep(30000) only if your node was waken up by interrupt.

                Documentation for the return value is available at https://www.mysensors.org/download/sensor_api_20#sleeping

                alexsh1A Offline
                alexsh1A Offline
                alexsh1
                wrote on last edited by
                #7

                @mfalkvidd That's got be the last nail in the coffin - thanks for your help!

                1 Reply Last reply
                0
                • alexsh1A Offline
                  alexsh1A Offline
                  alexsh1
                  wrote on last edited by
                  #8

                  I think the final product would be looking like this:

                  #define DUPLICATE_INTERVAL 30000
                  .....................
                  if (sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME)) {
                        motionDetected = true;
                        motionTrips++;
                        sleep(DUPLICATE_INTERVAL);
                      }
                  

                  Testing now

                  mfalkviddM 1 Reply Last reply
                  1
                  • alexsh1A alexsh1

                    I think the final product would be looking like this:

                    #define DUPLICATE_INTERVAL 30000
                    .....................
                    if (sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME)) {
                          motionDetected = true;
                          motionTrips++;
                          sleep(DUPLICATE_INTERVAL);
                        }
                    

                    Testing now

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

                    @alexsh1 I think you need to test för >= 0. The If statement will return true for anything non-zero so the current test will fail. Except that, the code looks great.

                    ileneken3I 1 Reply Last reply
                    0
                    • mfalkviddM mfalkvidd

                      @alexsh1 I think you need to test för >= 0. The If statement will return true for anything non-zero so the current test will fail. Except that, the code looks great.

                      ileneken3I Offline
                      ileneken3I Offline
                      ileneken3
                      wrote on last edited by
                      #10

                      @mfalkvidd

                      This worked well for me, for a vibration sensor:

                      if (sleep(digitalPinToInterrupt(MOTION_INPUT_PIN), CHANGE, SLEEP_TIME) != -1) {
                      // Interrupt occurred, do whatever is needed and then
                      // clear any "floating" interrupts before going on.
                      while (sleep(digitalPinToInterrupt(MOTION_INPUT_PIN),CHANGE,1000) != -1 ) {
                      Serial.println("Clearing interrupts");
                      }
                      }

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


                      26

                      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