Button Actuated relay with Motion



  • I'm trying to make a Relay with a Switch and Motion. Here is my sketch it works from the controler fine (Vera). The problem if I make changes at the switch the controller does not update. Also I can not get the motion to work. I put the motion inside of a void section. I also tried the motion code inside of the loop also. Any help?

    // Example sketch för a "light switch" where you can control light or something 
    // else from both vera and a local physical button (connected between digital
    // pin 4 and GND).
    // This node also works as a repeater for other nodes
    // Added Motion detection
    // Modified by DrJeff On 10/18/15
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN  3  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  4  // Arduino Digital I/O pin number for button 
    #define LIGHT_CHILD_ID 1   // Id of the sensor child
    #define RELAY_ON 0
    #define RELAY_OFF 1
    
    #define MOTION_SENSOR 5   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT MOTION_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define MOTION_LIGHT_CHILD_ID 2   // Id of the sensor child
    #define NODE_ID 4
    
    uint8_t lastMotion = 0;
    unsigned long previousMillis = 0; // last time update //see http://stackoverflow.com/questions/10773425/performing-a-function-after-x-time for more details on this
    unsigned long motionDelay = 10000; // interval at which to keep motion sensor trippped (milliseconds).  Used to prevent too frequent updates to Vera. 
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    MySensor gw;
    MyMessage msgLIGHT(LIGHT_CHILD_ID,V_LIGHT);
    MyMessage msgMOTION(MOTION_LIGHT_CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      gw.begin(incomingMessage, NODE_ID, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay, Motion, & Button", "1.1");
    
      //Motion
     pinMode(MOTION_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      // Register all sensors to gw (they will be created as child devices)
      gw.present(MOTION_LIGHT_CHILD_ID, S_MOTION);
     
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(LIGHT_CHILD_ID, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(LIGHT_CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
      if (value != oldValue && value==0) {
          gw.send(msgLIGHT.set(state?false:true), true); // Send new state and request ack back
      }
      oldValue = value;  
    }
    
     void MotionStatus (){
       // Read digital motion value
       unsigned long currentMillis = millis();
      
         if(currentMillis - previousMillis > motionDelay){
          uint8_t motionDetect = digitalRead(MOTION_SENSOR);
      if(motionDetect != lastMotion){
      gw.send(msgMOTION.set(motionDetect?"1":"0"));
      if(motionDetect == 1){
        previousMillis = currentMillis;
      }
      else{
        previousMillis = currentMillis - motionDelay + 1000;
      }
      lastMotion = motionDetect;
        
      }
      }
     }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(LIGHT_CHILD_ID, state);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
        
       } 
    }
    

    I'm sure I'm doing something wrong just not sure what and were. Thanks for the help.


  • Contest Winner


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts