fails to wake with 2 interupts



  • hi all im having issue with the follow code...

    i have two devices on pin 2 and 3 on a pro mini but cant seem to get wake working one both...
    is anyone able to hint/advise on have gone wrong?

    /*
     * 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-2019 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Motion Sensor example using HC-SR501
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <SPI.h>
    
    
    #define COMPARE_TEMP 0
    #define MAX_ATTACHED_DS18B20 1
    #define DOOR_PIN  3
    #define ONE_WIRE_BUS 5 // Pin where dallase sensor is connected
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=1;
    bool receivedConfig = false;
    bool metric = true;
    
    
    
    uint32_t SLEEP_TIME = 900000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 2   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID_MOTION 1   // Id of the sensor child
    #define CHILD_ID_TEMP 2
    #define CHILD_ID_DOOR 3
    // Initialize motion message
    MyMessage motion(CHILD_ID_MOTION, V_TRIPPED);
    MyMessage temp(CHILD_ID_TEMP,V_TEMP);
    MyMessage door(CHILD_ID_DOOR,V_TRIPPED);
    
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 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
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    void before()
    {
      // Startup up the OneWire library
      sensors.begin();
    }
    
    
    
    void setup()
    {
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    
    pinMode(DOOR_PIN, INPUT);
      // Activate internal pull-up
      digitalWrite(DOOR_PIN, HIGH);
    
    
      
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Front Hall Motion&temp", "1.0");
    
        // Register all sensors to gw (they will be created as child devices)
        present(CHILD_ID_MOTION, S_MOTION);
        present(CHILD_ID_TEMP, S_TEMP);
        present(CHILD_ID_DOOR, S_DOOR);
    }
    
    void loop()
    {
        // Read digital motion value
        bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
        Serial.print("Montion Sensor State: ");
        Serial.println(tripped);
        send(motion.set(tripped?"1":"0"));  // Send tripped value to gw
    
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
    
     bool trippeddoor = digitalRead(DOOR_PIN) == HIGH;
        Serial.print("Door Sensor State: ");
        Serial.println(trippeddoor);
        send(door.set(trippeddoor?"1":"0"));  // Send tripped value to gw
        
        
    sensors.requestTemperatures();
      Serial.print("Temprature: ");
      Serial.println(sensors.getTempCByIndex(0));
      // query conversion time and sleep until conversion completed
      // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
      wait(500);
    
      // Read temperatures and send them to controller 
    
        // Fetch and round temperature to one decimal
        float temperature = sensors.getTempCByIndex(0);
        
          // Send in the new temperature
          send(temp.setSensor(1).set(temperature,1));
          // Save new temperatures for next compare
    
          batM();
          delay(500);
          sleep(3000);
        sleep(DIGITAL_INPUT_SENSOR,CHANGE,DOOR_PIN,CHANGE,SLEEP_TIME);
    }
    
    
    void batM() //The battery calculations
    {
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
     
    


  • @markjgabb said in fails to wake with 2 interupts:

    #define DIGITAL_INPUT_SENSOR 2
    #define DOOR_PIN  3
    [...]
    void loop()
    {
        sleep(DIGITAL_INPUT_SENSOR,CHANGE,DOOR_PIN,CHANGE,SLEEP_TIME);
    }
    

    Use the function digitalPinToInterrupt(). It translates the pin number (2, 3) to the corresponding interrupt vector (0, 1).

    sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE,
          digitalPinToInterrupt(DOOR_PIN), CHANGE,
          SLEEP_TIME);
    


  • @markjgabb said in fails to wake with 2 interupts:

    pinMode(DOOR_PIN, INPUT);

    to be a true pull up your the code should read pinMode(DOOR_PIN, INPUT_PULLUP);



  • @markjgabb Two great bits of advice from @BearWithBeard and @mntlvr .

    Put them both in your code and you should be good to go!



  • thanks guys, ill add it to my code tonight and see how it goes


Log in to reply
 

Suggested Topics

  • 4
  • 10
  • 6
  • 6
  • 2
  • 2

207
Online

9.7k
Users

10.3k
Topics

106.4k
Posts