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. My Project
  3. Button Actuated relay with Motion

Button Actuated relay with Motion

Scheduled Pinned Locked Moved My Project
2 Posts 2 Posters 1.8k 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.
  • DrJeffD Offline
    DrJeffD Offline
    DrJeff
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TheoL
      Contest Winner
      wrote on last edited by
      #2

      Maybe this will help you a bit http://forum.mysensors.org/topic/2106/reinventing-the-motion-controlled-outside-lamp

      1 Reply Last reply
      0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      21

      Online

      12.0k

      Users

      11.2k

      Topics

      113.4k

      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