Question about implementing Interrupt for stopping loop

const byte GreenPushButtonPin = 39;

const byte FrontDoorPin = 20;
const byte SideDoorPin = 21;
const byte SelSwitchPin = 35;

byte GreenPushButtonState;
byte FrontDoorState;
byte SideDoorState;
volatile boolean Safe;

void setup() {

  pinMode(GreenPushButtonPin, INPUT_PULLUP);

  pinMode(FrontDoorPin, INPUT_PULLUP);
  pinMode(SideDoorPin, INPUT_PULLUP);


  attachInterrupt(digitalPinToInterrupt(FrontDoorPin), Doors_Open, HIGH);
  attachInterrupt(digitalPinToInterrupt(SideDoorPin), Doors_Open, HIGH);

}


void Doors_Open() {
  Safe = false;
}

void loop() {
  if (Safe) {
 //code for running loop only if doors are closed.
    }
  }

Hello Everyone

My question is about implementing interrupt and ISR. I want some motors to run inside a case, only if doors are closed. Since some parts of code use Delay,so I wanted to use interrupt.

My question: ' Is "if (safe)" a good choice infront of the loop code or some other function like while or for would be a good choice.

This ISR isnt working yet for me. I just want the loop to stop, if doors are open

Thanks

IF you are using "delay() inside your loop, using an interrupt will not help because there is noting you can do in an interrupt to stop the motor. Set a Boolean to TRUE in your interrupt and test for it in your loop, but get rid of the "delay()".

Paul

First of all, "Safe" must be initialized to "true" if the posted code is to make any sense. Second, the ISR will not interrupt the loop and therefore it does not matter if you use the ISR or if you just check the pin(s) in the loop. If you want to be able to react faster on door opening, you must remove any use of delay which will enable you to poll door state much more frequently.

Mani5678:
My question is about implementing interrupt and ISR. I want some motors to run inside a case, only if doors are closed. Since some parts of code use Delay,so I wanted to use interrupt.

That's not the right way to deal with the problem.

You need to replace the delay()s with non-blocking code using millis() and then you won't have any problem stopping the motors whenever you want.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Thanks for your input everyone, I will work upon removing the delay functions