Repeater and motion



  • The sketch works only it is sending the motion every second. I only want the sensor to send if there is motion. Who can help me with the code?

    #include <MySensor.h>  
    #include <SPI.h>
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      // The third argument enables repeater mode.
      gw.begin(NULL, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Motion Sensor/Repeater", "1.0");
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_MOTION);
      
    }
    
    void loop()     
    {     
      // By calling process() you route messages in the background
      gw.process();
      
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
     
    }
    

  • Hardware Contributor

    Is your sensor giving you a HIGH reading all the time?



  • @insomnia

    Your sketch has the motion sensor sending its status every time through the loop, add a qualifier to only send when the status changes:

    boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
      if (tripped != lastTrippedState)
      {  
        Serial.println(tripped? "tripped" : "not tripped");
        gw.send(msg.set(tripped?"1":"0"));// Send tripped value to gw//
        lastTrippedState = tripped
      }
    


  • @Dwalt That was what i needed. It works now 🙂


Log in to reply
 

Suggested Topics

  • 3
  • 1
  • 1
  • 5
  • 2

17
Online

11.4k
Users

11.1k
Topics

112.7k
Posts