Trying to understand noInterrupts()

So it seems that if I block out a piece of code with noInterrupts(), and attempt to interrupt, that it does interrupt.

Near as I can tell, does noInterrupts() not turn interrupts off, but just keeps the code from executing until interrupts() is called?

As in, it queues up, but doesn’t execute?

Here is the code I’m playing with, where I turn on the Argon’s light at D7, and enter a 10 second loop. If I hit my button while the light is on, I’ll get serial output, after the light goes out, of Button Presses: 1

Which makes me think it queues up the interrupt, but doesn’t execute it. If so, how I would not even let it queue up?

I’ve had some struggle with detachInterrupt and then attachInterrupt.

#include "application.h"

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);

uint8_t ButtonPressCount = 0;
system_tick_t LastButtonPress = 0;

void DoButtonPressEvent(void) 
{
    if ((millis() - LastButtonPress) > 250)    
        ButtonPressCount++;            
}

void setup(void)
{    
    pinMode(D7, PinMode::OUTPUT);    
    digitalWrite(D7, PinState::LOW);
    
    LastButtonPress = millis();    
    
    Serial.begin();    
    
    noInterrupts();    

    pinMode(D6, PinMode::INPUT);
    attachInterrupt(D6, DoButtonPressEvent, InterruptMode::RISING);

    digitalWrite(D7, PinState::HIGH);

    do 
    {
        // kill time for 10 seconds
    } while((millis() - LastButtonPress) < 10000);
            
    digitalWrite(D7, PinState::LOW);

    interrupts();   
}

void loop(void)
{    
    if (ButtonPressCount > 0)
        Serial.printlnf("Button presses: %d", ButtonPressCount);
}

You should declare a global variable like:

volatile bool counterActive = false;

In your DoButtonPressEvent() function, check the counterActive flag. If false, return immediately.

Remove the noInterrupts() and interrupts() calls and just use counterActive. It’s also a bad idea to disable interrupts for more than a millisecond or two, so avoid doing that in general.

You could also detach the interrupt when not in use, but the flag variable is usually more efficient.

Thanks, I’ll use the flag and avoid noInterrupts.

But, for understanding reasons, what’s the purpose of noInterrupts if it still allows interrupts?

The ISR is not called during the time between calling noInterrupts() and calling interrupts() again. This allows timing or resource-sensitive code to not be interrupted during that period.

Interrupts are serviced when reenabled after calling interrupts(). This is normally what you want if you are servicing an interrupt from an external device, say for example a SPI UART. You need to service the interrupt to read the data and clear the interrupt flag, so discarding the interrupt would be the wrong thing in that case.

Thanks much!