Best way to play nice with interrupts

So, I have a project that is using interrupts to catch pulses from a flowmeter (SF800). This all works really well, well maybe too well. The interrupts can come in long bursts as you might imagine which appear to prevent the main loop code from executing until there is a lull. Frequently, the core loses it’s connection to the cloud due to this and that causes a reset.
Is there a suggested way to yield during an interrupt to make sure things don’t go south? I expect most “events” to last 5-10s, so it shouldn’t be an issue, but I have seen it go pear shaped with 5s bursts.

BTW, the code I am using is:
attachInterrupt(0, meterInterrupt, FALLING);

billprozac, what does your code do in the ISR? Perhaps include the code for meterInterrupt()?

Sure, well, I have been toying around with the location of certain parts of my ligic, but this is the gist:

void meterInterrupt()
{
    pourtimer=millis();
    meter += 1;
    pouring=true;
    activity();
}

and activity is

void activity(){
  digitalWrite(KB_PIN_LED,HIGH);
  delay(10);
  digitalWrite(KB_PIN_LED,LOW);
  acttime = millis();
}

so the whole logic block does have 10ms of delay. I have not tried removing that delay to see if that would improve things. I guess that gets to the heart of the question and that is how does the interrupt get handled in the context of what is going on with the core?
Also, sine I am a bit green, can an interrupt interrupt another interrupt if it running too long? If I wanted to do something that would take sometime, like update a display, could I do that in the interrupt or is that a terrible idea?

billprozac, your ISR is doing too much and taking too much time for high flowrates. Oh and that delay is a MAJOR no-no in an ISR! (LOL) I have experience with water flow sensors and I helped someone else in this topic.

The gist of it is your ISR simply counts water sensor pulses and your loop() samples that counter every 1s. From there, it is easy to calculate the flowrate, the volume, etc. Also, when the counter is sampled in loop() and it is not zero, you could blink an activity LED.

The SMT32 has an interrupt priority manager but it is always best to keep code short and simple. Let me know if you need help. Nice sensor by the way! :smile:

1 Like

Thanks, I’ll get to work cleaning that up. I am so used to programming in a multi-threaded environment, that I am having to learn some real important lessons here :wink:

1 Like

I know the feeling ! :koala:

1 Like

BTW, after cleaning up the ISR, things have been pretty darn smooth. Thanks a ton!

1 Like