Please can you tell me where I have gone wrong here?

  • Hi all,

    I have a sketch to monitor a door/window and that tesed OK - Then I decided to add an anti-tamper switch to the box and whatever I try I cannot get it to work....

    Here is the code....

    // Enable debug prints
    // #define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    #define MY_RF24_PA_LEVEL RF24_PA_LOW  //Options are: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH or RF24_PA_MAX
    #include <MySensors.h>
    uint32_t SLEEP_TIME = 900000; // Sleep time between reports (in milliseconds) approx eveery 15 mins.
    #define DIGITAL_INPUT_SENSOR_2 2   // Anti-Tamper switch connection.
    #define DIGITAL_INPUT_SENSOR_3 3   // The digital input you attached your Door/Window sensor.
    #define CHILD_ID_1 2   // Id of the Anti-Tamper sensor child.
    #define CHILD_ID_2 3   // Id of the door/window sensor child.
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    int batteryPcnt = 0;
    // Initialize door/window message
    MyMessage msg( CHILD_ID_1, V_TRIPPED);  //Tamper.
    MyMessage msg1( CHILD_ID_2, V_TRIPPED); //Door/Window.
    void setup()
      pinMode(DIGITAL_INPUT_SENSOR_3, INPUT);      // sets the door/window sensor digital pin as input
      digitalWrite(DIGITAL_INPUT_SENSOR_3, HIGH);
      pinMode(DIGITAL_INPUT_SENSOR_2, INPUT);      // sets the Anti-Tamper sensor digital pin as input
      digitalWrite(DIGITAL_INPUT_SENSOR_2, HIGH);
    void presentation()
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Patio Door", "3.0");
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_1, S_DOOR, "Patio Tamper");
      present(CHILD_ID_2, S_DOOR, "Patio Door");
    void loop()
      ////to be tested ........../////
      // Read digital anti-tamper.
      bool tripped = digitalRead(DIGITAL_INPUT_SENSOR_2) == HIGH;
        send(msg.set(tripped ? "1" : "0")); // Send anti-tamper tripped value to gw
      //Read digital door/window value.
      bool tripped = digitalRead(DIGITAL_INPUT_SENSOR_3) == HIGH;
         send(msg1.set(tripped ? "1" : "0")); // Send door/window tripped value to gw
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
        batteryPcnt = sensorValue / 10;
      // Sleep until interrupt comes in on motion sensor. Send battery update every 15 minutes.
      sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_2) || digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_3), CHANGE, SLEEP_TIME);

    The door/window pin works and the data gets sent to the controller, but it never sees any data from the anti-tamper switch....I have tried a few things but I cannot get both switches to work - what have I got wrong here please?

  • Mod

    @skywatch the sleep call in the code is very strange. I think you want to use this function:

    int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);

    Reference for sleep:

  • Hi @Mfalkvidd -

    Yes I just tracked it down to that line with w few serial.prints..... Thanks for the fast reply - I will try again! 🙂


    sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_2), CHANGE, digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_3), CHANGE, SLEEP_TIME);

    This way it works! 🙂 - Thank you! 🙂

  • The alternative answer is that you chose a hobby that will take up all your spare time and burn all your extra money.

  • @wallyllama Ah, you mean marriage? Sorry, couldn't resist.... 🙂

  • @wallylama

    I already did that! 😮 - That's why I now need to make it all work!!! 🙂

Log in to reply

Suggested Topics