Looking for simple/clean Code example for sleep() external interrupt.



  • Hi

    Im starting to look into powering some nodes with battery.

    I have been looking for a minimalistic code example with external interrupt but I cant seem to find one that I feel confident to adapt to my own needs.

    I have a 2 switch node that works like a charm. The code is mainly (or was it completly) from this thread. 2 Reed switches 1 Arduino Nano

    I will use the code/node to log a doors state Open/closed, Locked/onlocked.

    I Would like for the node to sleep as "deep" as possible and when the state of either pin changes, run the code once (Check if the value on the door has change, if so send the data. Check if the value on the Lock has change, if so send the data.) then go back to deep sleep.

    Will a deep sleep power of the Transiver?

    Can anyone help me in the right direction?

    Code

    /**
    
        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
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_1 3
    #define BUTTON_PIN_1 3 // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID_2 4
    #define BUTTON_PIN_2 4 // Arduino Digital I/O pin for button/reed switch
    
    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();
    
    int oldValue1 = -1;
    int oldValue2 = -1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID_1, V_TRIPPED);
    MyMessage msg2(CHILD_ID_2, V_TRIPPED);
    
    void setup()
    {
      // Setup the button
      pinMode(BUTTON_PIN_1, INPUT);
      pinMode(BUTTON_PIN_2, INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_1, HIGH);
      digitalWrite(BUTTON_PIN_2, HIGH);
    
      // After setting up the button, setup debouncer
      debouncer1.attach(BUTTON_PIN_1);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON_PIN_2);
      debouncer2.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(CHILD_ID_1, S_DOOR);
      present(CHILD_ID_2, S_DOOR);
    }
    
    // Check if digital input has changed and send in new value
    void loop()
    {
      debouncer1.update();
      // Get the update value
      int value1 = debouncer1.read();
    
      if (value1 != oldValue1) {
        // Send in the new value
        send(msg1.set(value1 == LOW ? 1 : 0));
        oldValue1 = value1;
      }
    
      debouncer2.update();
      // Get the update value
      int value2 = debouncer2.read();
    
      if (value2 != oldValue2) {
        // Send in the new value
        send(msg2.set(value2 == HIGH ? 1 : 0));
        oldValue2 = value2;
      }
    }
    


  • I think the sketch you need is here .


Log in to reply
 

Suggested Topics

  • 4
  • 3
  • 8
  • 9
  • 2
  • 15

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts