Enable and disable interrupts in Arduino


If you wish to disable interrupts (when executing some critical piece of code, especially one which should be completed within a given time period), you can do that with the help of the noInterrupts() function.

Once your critical code has executed and you wish to re-enable the interrupts, you can do that using interrupts() function. Note that interrupts are enabled by default in Arduino, and therefore, calling interrupts() without an initial call to noInterrupts() is unnecessary.

Example

The general structure of a code containing noInterrupts() and interrupts() is given below −

void setup() {
   // put your setup code here, to run once:
}

void loop() {
   // put your main code here, to run repeatedly:

   noInterrupts();
   //Add critical code that needs to be completed in a specific time below

   interrupts();
   //Add non-critical code, that can tolerate interruptions, below

}

Updated on: 31-May-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements