# Watchdog Application

**URL:** <https://community.particle.io/t/watchdog-application/41841>\
**Category:** Troubleshooting\
**Created:** [May 15, 2018, 11:48am UTC](https://community.particle.io/t/watchdog-application/41841 "2018-05-15T11:48:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![electronweather](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@electronweather](https://community.particle.io/u/electronweather)\
**Post date:** [May 15, 2018, 11:48am UTC](https://community.particle.io/t/watchdog-application/41841/1 "2018-05-15T11:48:24Z")

</div>

Hi 🙂

This is a part of my code:

```cpp
ApplicationWatchdog wd(940000, System.reset,1536);

void setup()
{
  pinMode(WindSensorPin, INPUT);
  attachInterrupt(WindSensorPin, isr_rotation, FALLING); 
}

void loop() {  

  interrupts(); 
  delay (3000); // Wait 3 seconds to average
  noInterrupts(); 
  
wd.checkin();    

}

```

In the documentation I can see that interrupts have to be enabled, but I tested my code and after the code of my Electron crashed, it woke up after 15 mins (940000). Why did it work? 😅

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 15, 2018, 12:13pm UTC](https://community.particle.io/t/watchdog-application/41841/2 "2018-05-15T12:13:32Z")

</div>

Having interrupts turned off between iterations of `loop()` is no good idea.

As I already have pointed out to you in your other thread

> [@Davis anemometer and Electron](https://community.particle.io/t/davis-anemometer-and-electron/38068/16):
>
> Just one thing to be careful about. While an Arduino doesn’t do a lot else than running your code, Particle devices do other stuff while not occupied with your code (e.g. cloud house keeping) which might get affected by disabling interrupts for prolonged periods.

You should only disable interrupts for _very_ short times, e.g. only for a couple of instructions to transfer highly volatile variables.  
Instead of using `noInterrupts()` I'd rather use [`ATOMIC_BLOCK`](https://docs.particle.io/reference/firmware/photon/#atomic_block-) for such cases.

---

<div class="post-metadata">

**Author:** ![electronweather](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@electronweather](https://community.particle.io/u/electronweather)\
**Post date:** [May 15, 2018, 12:30pm UTC](https://community.particle.io/t/watchdog-application/41841/3 "2018-05-15T12:30:13Z")

</div>

The problem is that the wind sensor counts the interrupts on a specific pin during 3 seconds and then calculate the wind speed. How can I ensure that the Particle does only count the interrupts in the 3 seconds?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 15, 2018, 12:41pm UTC](https://community.particle.io/t/watchdog-application/41841/4 "2018-05-15T12:41:01Z")

</div>

You can have a “guard flag” which you turn on and off when you want to take the counts.  
But the better approach for high speed interrupts would be to turn the logic round and rather measure the time for - lets say - 100~1000 triggers.  
Or even better, depending on your trigger frequency choose one or the other to get the best results.

---

<div class="post-metadata">

**Author:** ![electronweather](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@electronweather](https://community.particle.io/u/electronweather)\
**Post date:** [May 15, 2018, 1:18pm UTC](https://community.particle.io/t/watchdog-application/41841/5 "2018-05-15T13:18:34Z")

</div>

Is there a possibility to disable interrupts only for one pin?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 15, 2018, 1:26pm UTC](https://community.particle.io/t/watchdog-application/41841/6 "2018-05-15T13:26:12Z")

</div>

Yes, by calling `detachInterrupt()` 😉  
You could of course also dig into the HW specifics of the STM32F2 to clear/set the interrupt mask register bit, but the above just seems simpler.

However I’d still consider this less useful than my previously proposed approach(es).

Especially since you are dealing with fuzzy data (wind speed isn’t that exact and constant) the implication of missing the odd impulse won’t affect the over all outcome too much (pragmatically reasonable vs. theoretically possible).

---

<div class="post-metadata">

**Author:** ![electronweather](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@electronweather](https://community.particle.io/u/electronweather)\
**Post date:** [May 15, 2018, 2:02pm UTC](https://community.particle.io/t/watchdog-application/41841/7 "2018-05-15T14:02:18Z")

</div>

Ok 🙂

During the loop interrupts are disabled for approximately 60 ms. Does this effect the watchdog application?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [May 15, 2018, 2:09pm UTC](https://community.particle.io/t/watchdog-application/41841/8 "2018-05-15T14:09:43Z")

</div>

Not the watchdog, but the cloud tasks as they are being executed **_between_** iterations of `loop()` and your code disables interrupts before it leaves `loop()` and reenables them inside `loop()` so exactly when the cloud tasks would need to have interrupts enabled you have them disabled.
