Pages: [1]   Go Down
Author Topic: attachInterrupt, detachInterrupt, re-attachInterrupt  (Read 1431 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm now studying the function of interrupts...I need it for my prototype which deals with security..so I hve to enable, disable, and re-enable interrupts depending on the situation...

I was able to use the interrupt and execute the Interrupt Service Routine (ISR) properly...I was also able to detach the interrupt using detachInterrupt function...

When I don't trigger the Interrupt Pin of Arduino once the interrupts are detached, the ISR is not executed (as expected). When the interrupt is re-attached using attachInterrupt (without triggering the interrupt pin during the detachInterrupt), the ISR is NOT executed at all, only until I trigger the interrupt pin...

Now here's my problem, when I trigger the Interrupt Pin of Arduino while the interrupts are detached, ISR is not executed...but when I re-attach the interrupt using the attachInterrupt, the program executes the ISR right after attaching the Interrupt, even if I did not trigger the Interrupt Pin yet...

How can I stop the ISR from executing, everytime I re-attach the Interrupt? Help please...

Here's my ' experimental ' code in exploring Interrupt...

Code:
int pin = 13;
volatile int alarm = 0;

int x =0;
int cnt =0;

int new_count =0;

void setup()
{
  Serial.begin(9600);
  pinMode(pin, OUTPUT);
  attachInterrupt(1, blink, FALLING);
  attachInterrupt(0, blink, FALLING);
}

void loop()
{
  if (alarm == 1){
    Serial.println("Intruder Alert!!!!");
    Serial.print("CNT=");
    Serial.println(cnt);
    Serial.println();
    delay(1000);
    alarm = 0;
    if(cnt>=5){
  /*********************************************
  If interrupt is triggered 5 times, interrupt will be disabled
  **********************************************/
      detachInterrupt(0);
      detachInterrupt(1);
      Serial.println("\n\nInterrupts disabled.");
      new_count++;
    }
  }   

  digitalWrite(pin, LOW);

  if (new_count>0){
    new_count++;
    delay(10000);    //delay for few seconds before re-enabling the interrupt
    if (new_count>=6){
      Serial.println("\n\nInterrupt enabled again.");

      attachInterrupt(1, blink, FALLING);
      attachInterrupt(0, blink, FALLING);
     
      cnt=0;
      new_count=0;
    }
  }
 
}

void blink()
{
  digitalWrite(pin, HIGH);
  alarm = 1;
  cnt++;
}




Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The hardware buffers the interrupt, so it will be thrown.

possible solutions:
- clear the interrupt register before re-attaching - check the datasheet how
- set a time - t = micros(); - and ignore any call if time is within certain threshold.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I need it for my prototype which deals with security..so I hve to enable, disable, and re-enable interrupts depending on the situation...
Why? It seems to me that an event which triggers an interrupt, like the arrival of serial data, needs to ALWAYS trigger that interrupt. I think that you should probably leave the interrupts alone, and either care, or not, about the fact that they were triggered, as appropriate.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It seems to me that an event which triggers an interrupt, like the arrival of serial data, needs to ALWAYS trigger that interrupt
I do not agree 100% on this one. There can be many real live apps why one wants to enable or disable interrupts.

I once made an application using an TSLR235R - light to frequency converter - which overflows an Arduino with interrupts at higher light levels. Before that happened I disabled the interrupt and re-enabled it when light levels became lower. [switched to polling an LDR with analogRead() to detect low level].

Other reason could be if one wants to alternate between two sensors because both sensors simultaneous would interfere timing.

Or one wants just to make measurements for a short time, e.g. first 15 seconds after a weather balloon starts to drop again.

But that said, most sketches will not need to switch off interrupts
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the late reply...thank you so much 4 d help...finally I was able to clear the interrupt register by using

EIFR = 0xNN;    //external interrupt flag register

where NN is the hex equivalent of the interrupt flag register according to the manual/datasheet...hope others will also be guided with this post...

@robtillaart - thanks a lot for your help...  smiley
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19359
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Lots of info here:

http://gammon.com.au/interrupts
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: