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;
}
}