Motion sensor withput the interrupt


  • Hero Member

    Hello,
    I'm planning on building a node with motion sensor, light detector and a dimmer.
    For it to work, I guess I can't have the node to sleep, so it will be able to receive command from the gateway.
    So my question is how to adapt the motion sensor sketch to not use the interrupt.

    Would something like this work?

    void loop()    
    { 
    boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) 
    if(tripped== HIGH) sendTripped();
    
    //send light level if changed
    
    //dimmer control code
    }
    
    function sendTripped()
    {
    Serial.println(tripped);
    gw.sendVariable(CHILD_ID, V_TRIPPED, "1");  
    }

  • Plugin Developer

    The above should work, but i would also set an extra variable to check if there has been no movement at first. Otherwise it could continuously send movement.
    Just as example:

    boolean tripped = false;
    boolean prevTripped = false;
    
    void loop()    
    { 
        tripped = (digitalRead(DIGITAL_INPUT_SENSOR)==HIGH)?true:false;
        //// Only send when there has been no movement first.
        if(tripped==true and prevTripped==false) {
           sendTripped();
        }
        prevTripped = tripped;
        
        //send light level if changed
        
        //dimmer control code
    }
    
    function sendTripped()
    {
        Serial.println(tripped);
        gw.sendVariable(CHILD_ID, V_TRIPPED, "1");  
    }

  • Mod

    @ferpando You can just leave the interrupt in. It will no longer wake the Arduino from sleep (as it won't be sleeping) but the interrupt will still be handled on motion!


  • Hero Member

    Yes I thought so, but also thought the interrupt could mess with the rest of the sketch?
    If the node receives a message from gateway and an interrupt happens, how would it behave?


  • Hero Member

    @John
    It was just a quick proof of concept.. I guess I should set tripped to false once the message is sent


  • Plugin Developer

    @ferpando

    If the node receives a message from gateway and an interrupt happens, how would it behave?

    I asked almost the same question in an other thread, and i got the answer it is not interrupt save... I can not exactly say how it would behave.



  • Out of curiosity...how did you solve this?


  • Hero Member

    I think you are on the right track. There' s no need for the interrupt in your case. So you don't have to worry about what process is being interrupted.

    @ferpando said:

    It was just a quick proof of concept.. I guess I should set tripped to false once the message is sent

    John had the right logic. You don't want to set tripped false until the input (ie: motion dectector output) goes false, not when a message is sent.

    His suggested logic sends one message whenever the motion detector goes active, and not again until it goes inactive and active again.

    (Which happens after no motion for some time period adjustable via a pot on the motion detector).


Log in to reply
 

Suggested Topics

  • 87
  • 10
  • 3
  • 2
  • 7
  • 6

23
Online

11.2k
Users

11.1k
Topics

112.5k
Posts