Multiple motion/door sketch



  • I like to use store bought PIR motion sensors and have them trigger a Pam-1 relay due to their quality and being much safer. I’ve been using heks Door, Window and Push-button sketch with S_MOTION/S_DOOR. This way I only have to short one of the pins to ground.

    Well today I took the multi button relay sketch and combined it with heks sketch allowing me to have multiple motions on one pro mini.

    Here’s the sketch with two motions

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 12
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define SSR_A_ID 1   // Id of the sensor child
    #define SSR_B_ID 2   // Id of the sensor child
    
    const int buttonPinA = 2;
    const int buttonPinB = 3;
    
    int oldValueA = 0;
    int oldValueB = 0;
    
    bool stateA = false;
    bool stateB = false;
    
    Bounce debouncerA = Bounce();
    Bounce debouncerB = Bounce(); 
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgA(SSR_A_ID, V_TRIPPED);
    MyMessage msgB(SSR_B_ID, V_TRIPPED);
    
    void setup()  
    {  
      // Setup the button
      pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
      pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
    
    
      // After setting up the buttons, setup debouncer
      debouncerA.attach(buttonPinA);
      debouncerA.interval(5);
      debouncerB.attach(buttonPinB);
      debouncerB.interval(5);
    
    }
    
    void presentation() {
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(SSR_A_ID, S_MOTION);
      present(SSR_B_ID, S_MOTION);   
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop()
    {
      debouncerA.update();
       // Get the update value
      int valueA = debouncerA.read();
    
      if (valueA != oldValueA) {
         // Send in the new value
         send(msgA.set(valueA==HIGH ? 0 : 1));
         oldValueA = valueA;
        
      }
     
      debouncerB.update();
      // Get the update value
       int valueB = debouncerB.read();
    
      if (valueB != oldValueB) {
         // Send in the new value
         send(msgB.set(valueB==HIGH ? 0 : 1));
         oldValueB = valueB;
      }
    }```
    
    

    Here's one with three motions

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 11
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define SSR_A_ID 1   // Id of the sensor child
    #define SSR_B_ID 2   // Id of the sensor child
    #define SSR_C_ID 3   // Id of the sensor child
    
    const int buttonPinA = 2;
    const int buttonPinB = 3;
    const int buttonPinC = 4;
    
    int oldValueA = 0;
    int oldValueB = 0;
    int oldValueC = 0;
    
    bool stateA = false;
    bool stateB = false;
    bool stateC = false;
    
    Bounce debouncerA = Bounce();
    Bounce debouncerB = Bounce(); 
    Bounce debouncerC = Bounce();
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msgA(SSR_A_ID, V_TRIPPED);
    MyMessage msgB(SSR_B_ID, V_TRIPPED);
    MyMessage msgC(SSR_C_ID, V_TRIPPED);
    
    void setup()  
    {  
      // Setup the button
      pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
      pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
      pinMode(buttonPinC, INPUT_PULLUP); // Setup the button Activate internal pull-up
    
      // After setting up the buttons, setup debouncer
      debouncerA.attach(buttonPinA);
      debouncerA.interval(5);
      debouncerB.attach(buttonPinB);
      debouncerB.interval(5);
      debouncerC.attach(buttonPinC);
      debouncerC.interval(5);
    
    }
    
    void presentation() {
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(SSR_A_ID, S_MOTION);
      present(SSR_B_ID, S_MOTION);
      present(SSR_C_ID, S_MOTION);   
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop()
    {
      debouncerA.update();
       // Get the update value
      int valueA = debouncerA.read();
    
      if (valueA != oldValueA) {
         // Send in the new value
         send(msgA.set(valueA==HIGH ? 0 : 1));
         oldValueA = valueA;
        
      }
     
      debouncerB.update();
      // Get the update value
       int valueB = debouncerB.read();
    
      if (valueB != oldValueB) {
         // Send in the new value
         send(msgB.set(valueB==HIGH ? 0 : 1));
         oldValueB = valueB;
    
      }
    
      debouncerC.update();
      // Get the update value
       int valueC = debouncerC.read();
    
      if (valueC != oldValueC) {
         // Send in the new value
         send(msgC.set(valueC==HIGH ? 0 : 1));
         oldValueC = valueC;
      }
    }```


  • @bluezr1 Good progress!
    In case you are interested in how to shorten this kind of code and be more flexible in assigning PINs, you may have a look at this post here: https://forum.mysensors.org/topic/4847/multi-button-relay-sketch/33# by @korttoma
    Just delete the relay part...


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 2
  • 2
  • 1
  • 3

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts