Determine if pin interupt or awakens from sleep

  • Hi

    I have an magnetic door switch sensor with a DS18b20 conected. The sensor sends updates when the magnetic switch recieved updates(pin interupt) or 30min(awakens from sleep) has passed. It sends updates from both the magnetic switch and from the temperature sensor.

    Is it possible to determine which interupt that occured? I want to send temperature updates only when it awakens from sleep and send magnetic switch updates only when pin interupt happens.

    if (pin interupt)
    {do this}
    if (awkens from sleep)
    {do that}

  • Mod

    @blom yes, just look at the return value of the sleep function. Documentation:

  • Thanks! Worked like a charm!

    uint8_t interruptReturn;
    void loop()  {
    if (interruptReturn == 1){do this}
    if (interruptReturn == MY_WAKE_UP_BY_TIMER){do that}
    interruptReturn = sleep(INTERRUPT,CHANGE,SLEEP_TIME);

    Next question: Is it possible to retrive remaining sleep time when waking up on interrupt so sensor could resume on time
    left when it goes to sleep again?

  • Mod

    @blom Currently not.
    When implemented on AVR this would be a very coarse indication anyway (around 8 seconds uncertainty), but this might be acceptible for your use case.

  • @yveaux

    You wrote 8sec. Does the sensor wake up every 8sec and update a sleep count variable?

  • Mod

    @blom more or less... On avr sleeping is using the watchdog. Longest watchdog sleep time is 8 seconds. Every 8 seconds the node wakes up, checks if there is another 8 seconds to sleep and goes to sleep again. If less than 8 seconds remains it will sleep 4 seconds, or 2 etc until the complete sleep time has elapsed.
    If it wakes from interrupt and was told to sleep for 8 seconds, it doesn't know how far it was in this 8 seconds when it woke up, hence the maximum 8 seconds uncertainty.

  • Create a global variable and set to 0.
    In the interrupt handler subroutine, set it to 1.
    Check its status in the main program to determine if an interruption has occurred.

    if (my_global_variable == 1)
        my_global_variable = 0;
        {do this}   / Action if an interrupt has occurred
    if (awkens from sleep)
    {do that}```

  • @yveaux

    Is it possible to retrive how much time left on the watchdog sleep time when it awakens on interupt?

  • Mod

    @blom yes, if you hack the library or use tekka's experimental version at

Log in to reply

Suggested Topics

  • 4
  • 2
  • 3
  • 20
  • 274
  • 14