2

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;        
}
2
  • 1
    please add your setup(). what is interruptBin? what is frame? what is ACTIVATE?
    – Piglet
    Oct 29, 2019 at 12:12
  • @Piglet I added them now
    – SaTech
    Oct 29, 2019 at 12:21

1 Answer 1

3

You could check for activate in your interrupt handler:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.