Watchdog Application

Hi :slight_smile:

This is a part of my code:

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

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

void loop() {  

  delay (3000); // Wait 3 seconds to average


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? :sweat_smile:

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

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

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 for such cases.

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?

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.

1 Like

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

Yes, by calling detachInterrupt() :wink:
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).

1 Like

Ok :slight_smile:

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

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.