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
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()".
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.