Search code examples
arduinointerruptinterrupt-handlingatmega32

I can't clear external interrupts in Arduino Uno


I'm working on a project using an arduino uno board, I'm using an External interrupt tied to a switch I wanted this switch to work only if i sent an activation order to the board The Problem is that, if the switch is pressed before i send the command, i get a pressed state once i send the command, even the switch is not pressed, which mean the external interrupt save the state before i and retrieve it once i enable it here is a snippet of the code

volatile boolean  EX_INT = 0, activate = 0;
const byte interruptBin = 3;
const byte ACTIVATE = 0x55;
unsigned char frame[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36};

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptBin, INPUT_PULLUP);
}

void loop() {
  if(activate == 1){
    //EIMSK =0;  EIFR = 0; I tried to clear the last interrupt but with no effect
    activate = 0; EX_INT = 0;
    attachInterrupt(digitalPinToInterrupt(interruptBin),buttonPressed,RISING);  
    while(EX_INT != 1);
    EX_INT = 0; 
    Serial.write((uint8_t*)frame, sizeof(frame)); 
    detachInterrupt(digitalPinToInterrupt(interruptBin)); 
  }
}

void serialEvent(){
  while (Serial.available()){
    value = Serial.read();
    if(value == ACTIVATE)
      activate = 1;
  }
}

void buttonPressed()      
{  
  EX_INT = 1;        
}

Solution

  • You could check for activate in your interrupt handler:

    void buttonPressed()      
    {
      if(activate) {  
        EX_INT = 1;   
      }     
    }