Door switch - sleep



  • I use this sketch for a door switch and i wan't to make it battery powered and also wan't to make the arduino go to sleep. Is this possible and how do i do that?
    gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME); Doesn't work with interrupt in this sketch

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    
    #define CHILD_ID1 3
    #define BUTTON_PIN1  3  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID2 4
    #define BUTTON_PIN2  4  // Arduino Digital I/O pin for button/reed switch
    #define CHILD_ID3 5
    #define BUTTON_PIN3  5  // Arduino Digital I/O pin for button/reed switch
    
    
    
    MySensor gw;
    Bounce debouncer1 = Bounce(); 
    Bounce debouncer2 = Bounce(); // debouncer for the second switch
    Bounce debouncer3 = Bounce(); // debouncer for the third switch
    
    int oldValue1=-1;
    int oldValue2=-1; // second switch needs to have it's own old state
    int oldValue3=-1; // second switch needs to have it's own old state
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    unsigned long SLEEP_TIME = 900000; // Sleep time between reports (in milliseconds)
    int oldBatteryPcnt = 0;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID1,V_TRIPPED), msg2(CHILD_ID2,V_TRIPPED),msg3(CHILD_ID3,V_TRIPPED);
    
    
    
    void setup()  
    {  
      gw.begin();
    
       // use the 1.1 V internal reference
    #if defined(__AVR_ATmega2560__)
       analogReference(INTERNAL1V1);
    #else
       analogReference(INTERNAL);
    #endif
     
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Door Sensor Battery meter", "1.0");
    
     // Setup the button
      pinMode(BUTTON_PIN1,INPUT_PULLUP ); // You can assign pinmode and use pullup in one statement.
      pinMode(BUTTON_PIN2,INPUT_PULLUP);
      pinMode(BUTTON_PIN3,INPUT_PULLUP);
      
      // Activate internal pull-up
     // digitalWrite(BUTTON_PIN1,HIGH);
    //  digitalWrite(BUTTON_PIN2,HIGH);
    //  digitalWrite(BUTTON_PIN3,HIGH);
      
      // After setting up the button, setup debouncer1
      debouncer1.attach(BUTTON_PIN1);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON_PIN2);
      debouncer2.interval(5);
      debouncer3.attach(BUTTON_PIN3);
      debouncer3.interval(5);
    
      // 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.
      gw.present(CHILD_ID1, S_DOOR);
      gw.present(CHILD_ID2, S_DOOR);
      gw.present(CHILD_ID3, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      // get the battery Voltage
       int sensorValue = analogRead(BATTERY_SENSE_PIN);
       #ifdef DEBUG
       Serial.println(sensorValue);
       #endif
       
       // 1M, 470K divider across battery and using internal ADC ref of 1.1V
       // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
       // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
       // 3.44/1023 = Volts per bit = 0.003363075
       float batteryV  = sensorValue * 0.003363075;
       int batteryPcnt = sensorValue / 10;
    
       #ifdef DEBUG
       Serial.print("Battery Voltage: ");
       Serial.print(batteryV);
       Serial.println(" V");
    
       Serial.print("Battery percent: ");
       Serial.print(batteryPcnt);
       Serial.println(" %");
       #endif
    
       if (oldBatteryPcnt != batteryPcnt) {
         // Power up radio after sleep
         gw.sendBatteryLevel(batteryPcnt);
         oldBatteryPcnt = batteryPcnt;
       } 
      // Check if the first switch state has changed
      debouncer1.update();
      // Get the update value
      int value = debouncer1.read();
     
      if (value != oldValue1) {
         // Send in the new value
         gw.send(msg1.set(value==HIGH ? 1 : 0));
    //     gw.send(msg2.set(value==HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
    //     gw.send(msg3.set(value==HIGH ? 1 : 0));  // this is where you turn the third switch in your controller
         oldValue1 = value;
      }
      
      // Check if the 2nd switch state has changed
      debouncer2.update();
      // Get the update value
      value = debouncer2.read(); // no need to redeclare it (I removed int)
     
      if (value != oldValue2) {
         // Send in the new value
    //     gw.send(msg1.set(value==HIGH ? 1 : 0));
           gw.send(msg2.set(value==HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
    //     gw.send(msg3.set(value==HIGH ? 1 : 0));  // this is where you turn the third switch in your controller
         oldValue2 = value;
      }
      
      // Check if the third switch state has changed
      debouncer3.update();
      // Get the update value
      value = debouncer3.read(); // no need to redeclare it (I removed int)
     
      if (value != oldValue3) {
         // Send in the new value
    //     gw.send(msg1.set(value==HIGH ? 1 : 0));
    //     gw.send(msg2.set(value==HIGH ? 1 : 0)); // this is where you turn the second switch in your controller
         gw.send(msg3.set(value==HIGH ? 1 : 0));  // this is where you turn the third switch in your controller
         oldValue3 = value;
      }
       // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    } ```


  • @insomnia

    You need to declare your interrupts to use the interrupt-from-sleep function. Right now it looks like your sketch is sleeping 15 minutes and then checking your door sensors and going back to sleep..



  • Like this
    #define INTERRUPT DIGITAL_INPUT_SENSOR-3
    #define INTERRUPT DIGITAL_INPUT_SENSOR-4
    #define INTERRUPT DIGITAL_INPUT_SENSOR-5

    Is this correct?



  • @insomnia I can not see any defined interrupt or the interrupt related function in your code. Starting with the Arduino docs is often helpful why I suggest taking a look at https://www.arduino.cc/en/Reference/AttachInterrupt. For only one switch a hardware interrupt will work fine.

    If you need to monitor a few more switches and you use an Arduino with 328P processor there are only 2 hardware interrupts available. Then the pin change interrupts may help but the task is getting more complicated, see for example http://playground.arduino.cc/Main/PinChangeInterrupt but there are libraries like https://github.com/GreyGnome/EnableInterrupt (which I so far not have tested).

    Then there are ways of dealing with heaps of interrupts as well but that is far beyond my knowledge.


  • Hero Member

    @insomnia Take a look at this article as it describes how to use one interrupt pin for multiple swirches: http://www.gammon.com.au/forum/?id=11091.

    Cheers
    Al


  • Hardware Contributor

    @insomnia
    Have you seen the basic MySensors example BinarySwitchSleepSensor ?

    (Personally I use a longer button settle delay though.)


Log in to reply
 

Suggested Topics

55
Online

11.4k
Users

11.1k
Topics

112.7k
Posts