Stuck in INTERRUPT

Hi
When executing the interrupt service routine, is there any way to force the arduino to be stuck in the interrupt service routine?

Why would you want to do that ?

Yes.
But why would you want to?

UKHeliBob:
Why would you want to do that ?

I have a system working normally and there is a sensor detecting if there is an emergency. I want the arduino to to switch on an alarm and stop the operation of the system till I come and confirm the fault with a push button. Therefore, the code in the loop service routine runs the program in the normal operation while the ISR serves the emergency case.
Hope I could deliver it :slight_smile:

I'd rethink that technique if it were my project. There's probably a better way of doing it. But, more detail would be required to advise.

gfvalvo:
I'd rethink that technique if it were my project. There's probably a better way of doing it. But, more detail would be required to advise.

Don't you mind if you give me a hint please?

A more conventional way to do it would be to set a flag in your interrupt routine when your sensor detects an alarm. In loop, if the flag is set, do emergency stuff, otherwise run business as usual.

HassanAli23:
Don't you mind if you give me a hint please?

What sort of "emergency"?

The sort that threatens some sort of harm?

AWOL:
What sort of "emergency"?

The sort that threatens some sort of harm?

It's an irrigation system. If there is a leakage of water in the valve I want to receive a message on my mobile phone and at the same time I want to stop pump.

It's an irrigation system. If there is a leakage of water in the valve I want to receive a message on my mobile phone and at the same time I want to stop pump.

How will you detect the leak ?

So far it does not sound like you need to use an interrupt

UKHeliBob:
How will you detect the leak ?

So far it does not sound like you need to use an interrupt

I have a raindrops sensor and it will be installed at the bottom of the valve that is buried under the ground. What came on my mind is using interrupts.

UKHeliBob:
So far it does not sound like you need to use an interrupt

Second that. It DOES sound like you need to use a State Machine.

I assume that your irrigation system is providing timing control to turn the pump on and off. Are you by any chance using delays to achieve that?

In any event, it's about time to post the code you have.

wildbill:
I assume that your irrigation system is providing timing control to turn the pump on and off. Are you by any chance using delays to achieve that?

In any event, it's about time to post the code you have.

I'm gonna use a soil moisture sensor to measure the moisture and depending on the level I want, the valve will open and the pump is turned on. Yes, I've used a delay to stay for a while irrigating then close the valve and turn off the pump.

That delay is really the reason why you have had to consider interrupts. They are really not necessary as has already been mentioned.

You can instead rely on millis for timing and a state machine to control your system, which again appears above. Then you can be polling your rain sensor with sufficient speed just in loop.

If the rain sensor is wet from a leak and buried, how are you going to be able to restart the system without it immediately detecting the water and shutting down again?

wildbill:
That delay is really the reason why you have had to consider interrupts. They are really not necessary as has already been mentioned.

You can instead rely on millis for timing and a state machine to control your system, which again appears above. Then you can be polling your rain sensor with sufficient speed just in loop.

If the rain sensor is wet from a leak and buried, how are you going to be able to restart the system without it immediately detecting the water and shutting down again?

In the main loop function is running the process of detecting the moisture level and the irrigation. Once there is a leakage it goes to the interrupt and I want it to stay there and not exit the ISR till I reset it. The raindrops sensor is buried and I have to dry it before the reset.

HassanAli23:
Once there is a leakage it goes to the interrupt and I want it to stay there and not exit the ISR till I reset it.

Wait there for ever --

L1: goto L1;

or

while(1);

@HassanAli23,
Perhaps you missed this:

wildbill:
In any event, it's about time to post the code you have.

HassanAli23:
Hi
When executing the interrupt service routine, is there any way to force the arduino to be stuck in the interrupt service routine?

I smell an xy problem. The big clue is in the responses: all the oltimers going "why the hell would you want to do that?"

That's not what interrupts are for, and that's not how you use them. I'd bet that you are writing long procedural code rather than working with state machines.

HassanAli23:
I have a system working normally and there is a sensor detecting if there is an emergency. I want the arduino to to switch on an alarm and stop the operation of the system till I come and confirm the fault with a push button. Therefore, the code in the loop service routine runs the program in the normal operation while the ISR serves the emergency case.
Hope I could deliver it :slight_smile:

Unless your emergency sensor works by sending a microsecond pulse when the emergency starts, you don't need an interrupt for it.

What you need is to organise your code as a state machine. "currently in an emergency" is a state that the sketch can be in. The key to this is that every method in your code needs to execute quickly, it should not be sitting there for periods of time using delay(). the loop() should, at every instant, work out if if needs to switch something on or off right now and then finish.

loop() {
  if we are currently in emergency mode
    if the button has been hit
      turn things back on (although not the hoses, because the regular
        hose on/off code will turn on the ones that need to be on)
      turn off the emergency light
      we are no longer in emergency mode
    end
  else if oh shit, there's an emergency!
    stop all the things
    turn on the emergency light
    we are now in emergency mode
  else {
    iterate through all my hoses {
      if the hose is off, and it now needs to be on
        turn the hose on
     end

      if the hose is on, and it now needs to be off
        turn the hose off
      end
    }
  }
}