0

I am not quite experienced with Arduino and Interrupts. Below I am trying to disable interrupt once it got detected and enable it again when I start loop again. But in below code I am not able to enable it.

int L = 2;
volatile int done0=0;
volatile unsigned long leftT=0;

void recordL()
{
  if(done0 == 0)
  {
      leftT=micros();
  }
  done0 = 1;
  detachInterrupt(0);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(L,INPUT);
  Serial.begin(9600);
  Serial.println("Start..");
}



void loop() {
  // put your main code here, to run repeatedly:

  attachInterrupt(0,recordL,RISING);
  done0 = 0;

  while(!done0)
  {

  }

  Serial.println("Sound..");
  Serial.println(leftT);
  delay(1000);
  Serial.println("After Delay");
}

but I made small change in code. I shifted 'done0=0' above attachInterrupt() then it is working. Why is that happening? One more issue that I am facing here is when I enable it again one more saved value is coming. Does it store value when I have disabled it?

int L = 2;
volatile int done0=0;
volatile unsigned long leftT=0;

void recordL()
{
    if(done0 == 0)
    {
        leftT=micros();
    }
  done0 = 1;
  detachInterrupt(0);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(L,INPUT);
  Serial.begin(9600);
  Serial.println("Start..");
}



void loop() {
  // put your main code here, to run repeatedly:
  done0 = 0;      
  attachInterrupt(0,recordL,RISING);

  while(!done0)
  {

  }

  Serial.println("Sound..");
  Serial.println(leftT);
  delay(1000);
  Serial.println("After Delay");
}

Output for 2nd case (Sound is coming only once but I am getting sound after delay also):

   Start..
   Sound..
   2873000
   After Delay
   Sound..
   3873528
   After Delay
  • 1
    You likely need to read the specific processor specifications to understand what may happen when you disable and enable interrupts. There are many Arduino boards which use different processors. So a question about interrupts with out specifying which processor you are using is difficult to answer. I suspect the hardware for the processor you are using keeps track of interrupts while interrupts are disabled. And will try to service them when you enable interrupts once again. But this is really up to how the processor's hardware works. – st2000 Nov 18 '17 at 15:04
  • Write down the output you got for the second case. – Mitu Raj Nov 18 '17 at 16:02
  • @MITURAJ I inserted in question. – MeetR Nov 18 '17 at 16:07
  • Are you using a pushbutton to give interrupt ? – Mitu Raj Nov 18 '17 at 16:47
  • No sound from sound sensor. – MeetR Nov 18 '17 at 16:49
1

The code needs modifications. I presume you are using digital pin 2 as your interrupt pin here. But the first argument to your attachinterrupt () is simply 0. That's not a recommended way of passing argument. Check the below link.

Check -https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

I think this is what happened -

In the first case, the code is stuck in the infinite while loop as interrupt routine might have called just after attachinterrupt() and done became 1. After that done becomes 0 and got stuck in while (!done) loop forever as interrupts are now disabled.

In the second case, after making done = 0, the code entered into the interrupt routine and made done = 1, comes out, skips while loop and prints output. Again in loop(), done is made 0 and interrupts are attached back. And again interrupt routine is executed again , possibly because it detected another rising edge. This issue is there with push buttons, due to bouncing. Here, sound sensor may not be outputting clean digital signal.

Note: Disabling interrupt inside interrupt routine is meaningless cz arduino by default disables all interrupts and queues further interrupts during the execution of an interrupt routine. So I really doubt its behavior. You better rectify it by removing that statement from there and put it in the main code after while().

0

Normally, an Arduino interrupt attachment is made in the setup() method and is never removed. Consider moving your call to attach the interrupt accordingly and removing your call to detach the interrupt.

Your approach is understandable when considering normal embedded processor programming. In such cases all aspects of the hardware must be considered. This may include resetting the interrupt hardware for the next interrupt event.

When programming in the Arduino paradigm, many details are abstracted from the coder to make the coding experience simple and straight forward. In the example found at the bottom of the interrupt attachment Arduino Reference page, it is evident that no code needs to be added to manage the interrupt within the interrupt method.

To address your question specifically (why does the 2nd code listing work while the 1st does not) consider the actual (possible) sequence of events. The setup() method is 1st run followed by the loop() method. In the 1st code listing you attached the interrupt then set your "done" flag allowing the code in the interrupt to run. You cause the interrupt by raising the input on interrupt pin 0. This will likely work and you will then disconnect the interrupt. If you try to cause the interrupt again before 1 seconds elapses it will appear that the interrupt did not work because the loop() method is still waiting for the 1 second delay you have added and has not re-attached the interrupt. After the 1 second delay has ended, the loop() method will be called again. It is likely this time the processor's hardware remembers you tried to cause an interrupt. So when the interrupt is attached, the interrupt routine is called. But the "done" flag has not yet been set to zero so the interrupt does not execute code controlled by the "done" flag. In the 2nd code listing you have remedied this problem by setting the "done" flag before reattaching the interrupt.

  • Why output is changing if I change position of 'done0 = 0'? – MeetR Nov 18 '17 at 15:28
  • Sorry didn't get it. Whichever value done flag has at the end of loop, loop will execute further from start and re-attach interrupt. Why that won't happen? – MeetR Nov 18 '17 at 16:21
0

Below I am trying to disable interrupt once it got detected and enable it again when I start loop again.

not sure why you want to do that but interrupts are DISABLED in isr by hardware (unless you explicitly enable it in isr) so what you are trying to do is done for you automagically.

But in below code I am not able to enable it.

mostly it involves enabling the respective interrupt mask, unless you want to enable / disable all interrupts.

  • Actually I want to take sound once and shut that interrupt for rest of the code in loop and enable that again. – MeetR Nov 18 '17 at 16:24

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.