Enabling & Disabling an Interrupt

I’m an experienced programmer and have worked with a number of microprocessors, but I’m new to the Arduino. I’m experimenting with interrupts and want to enable an interrupt in one section of the main loop, but disable it in another. I created a simple circuit with a red LED connected to pin 4 and a green LED to pin 5, plus a NO momentary contact switch to pin 2 for the interrupt (with internal pull-up enabled and connected so it goes low when pushed). The interrupt is programmed to trigger on FALLING. In the main loop I flash the red LED on for 2 seconds and off for 2 seconds, which allows me to press the interrupt button a couple of times in each state. The ISR simply toggles the green LED. To test the ability to enable and disable the interrupt in different sections of code, I disable the interrupt using detachInterrupt during the RED LED ON time and enable it during the RED LED OFF time using attachInterrupt. While I wasn’t sure that’s how to do it, everything works fine, except for one thing. Pressing the interrupt button while disabled (RED LED ON state) does not cause the green LED to toggle and pressing the button while enable (RED LED OFF state) does, indeed, toggle the green LED. However, if I press the button during the RED LED ON state, the green LED toggles when the main loop enters the RED LED OFF state. So, what appears to happening is that the interrupt event (button press) is registered, but not recognized while detached, but is recognized when it is re-attached. Other processers I’ve used have a bit in a register associated with the interrupt that you can use to mask and unmask the interrupt to avoid this problem. Does anyone know how to do this on the Arduino? I’m using an Arduino UNO, if it matters.

/*******************************************************************************
Program Name: enable_interrupt_demo

Programmer: Lew Rakocy
Date: 02/21/2020

Description:
  Main loop flashes RED LED ON for 2 seconds and off 2 seconds
  (allows time to press interrupt button a couple times in each state)
  Push button interrupt toggles GREEN LED but is enabled only during RED LED OFF

Pin Assignments:
  2 Push button interrupt (Pulled High, interrupt on FALLING)
  4 RED LED - light on HIGH
  5 GREEN LED - light on HIGH

Tested: 02/21/2020

******************************************************************************/

// Global constants
const byte interruptPin = 2;
const byte redLEDPin = 4;
const byte greenLEDPin = 5;

// Global variable
volatile byte greenLEDState = LOW;

void setup(){
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), toggle, FALLING);
}

// Flash red LED 2 sec on, 2 sec off
// (Allows time to press interrupt button a couple times in each state)
void loop( ){
  // Disable interrupt during RED LED ON state
  detachInterrupt(digitalPinToInterrupt(interruptPin));
  digitalWrite(redLEDPin, HIGH);
  delay(2000);

  // Enable interrupt during RED LED OFF state
  attachInterrupt(digitalPinToInterrupt(interruptPin), toggle, FALLING);
  digitalWrite(redLEDPin, LOW);
  delay(2000);
}

// Toggles green LED if button is pushed and interrupt enabled
void toggle(void){
  if(greenLEDState == LOW){
    // Toggle it HIGH
    digitalWrite(greenLEDPin, HIGH);
    greenLEDState = HIGH;
  }
  else{
    // Must be HIGH, so toggle it to LOW
    digitalWrite(greenLEDPin, LOW);
    greenLEDState = LOW;
  }
}

LRakocy:
However, if I press the button during the RED LED ON state, the green LED toggles when the main loop enters the RED LED OFF state.

Your description is very hard to absorb because it is all written as a single paragraph even though it contains several different thoughts.

I suspect the problem is that there is an un-tended interrupt in the pipline when the interrupt is attached. It is a good idea to clear the relevant interrupt flag immediately before enabling an interrupt. The EIFR register deals with this.
EIFR = 0b00000011;
will clear both interrupt flags. See the Atmega 328 datasheet.

...R

When we execute an attachInterrupt() instruction, the concerned IO lines becomes an interrupt line. The attachInterrup() also enables the local and global interrupt soft switches.

The detachInterrupt() instruction unfolds the action of attachInterrupt()--meaning that the concerned IO lines loses it interrupt capability and takes back the role of IO line.

Therefore follow @Robin2 suggestion and use EIFR EIMSK Register to disable a particular interrupt for which you need to consult the data sheets to see which bit controls (enable/disable) which interrupt. EDIT

GolamMostafa:
Therefore follow @Robin2 suggestion and use EIFR Register to disable a particular interrupt for which you need to consult the data sheets to see which bit controls (enable/disable) which interrupt.

That's not actually what I said and the EIFR register does not enable or disable interrupts.

The EIMSK register is the one that controls whether an interrupt is active.

...R

Robin2:
That's not actually what I said and the EIFR register does not enable or disable interrupts.

The EIMSK register is the one that controls whether an interrupt is active.

You are right; I am editing my post. EIFR is the 'External Interrupt Flag Register' to register the occurrences of interrupts; EIMSK is the 'External Interrupt Mask Register' to enable/disable local interrupts.

GolamMostafa:
When we execute an attachInterrupt() instruction, the concerned IO lines becomes an interrupt line.

Nope, the lines are always interrupt lines. attachInterrupt registers the passed function to handle the interrupt.

GolamMostafa:
The attachInterrup() also enables the local and global interrupt soft switches.

Nope, it enables the respective interrupt by manipulating a hardware flag
and setting the interrupt mode in the hardware,
it leaves the global flag alone, which is also a hardware flag.

GolamMostafa:
The detachInterrupt() instruction unfolds the action of attachInterrupt()--meaning that the concerned IO lines loses it interrupt capability and takes back the role of IO line.

Nope, the lines are always interrupt lines.
It disables the respective interrupt by manipulating a hardware flag, again leaving the global flag alone.