external interrupt, detach interrupt, clear flag, falling edge vs low level

Hey folks,

im suffering with a little problem here with external interrupts. Im using some bottons to trigger external interrupts. okay there might be problems like bouncing etc but in my case this should be the problem.

the interesting part of this program looks like

void drive()
{
  for(int x=0; x<4; x++)
{
    switch(x)
    {    
    case 0:     //       
    interruptNr = 1;
    break;
    case 1:     //       
    interruptNr = 0;
    break;
    case 2:     //       
    interruptNr = 5;
    break;
    case 3:     //       
    interruptNr = 4;
    break;
    case 4:     //       
    interruptNr = 3;
    break;
    }
    
if (x<4)
{
    digitalWrite(pinDir_[x],LOW);
    attachInterrupt(interruptNr, Stop, LOW);   
    while(c< u)
    {
     digitalWrite(pinStep_[x], HIGH);
      delayMicroseconds(8000);
      digitalWrite(pinStep_[x], LOW);
      delayMicroseconds(8000);
      c++;
    }
delay(1000);

}
}
}

void Stop()
{
 c=u;  
 detachInterrupt(interruptNr); 
}

So as you can see i use in this case 4 different buttons to stop a forloop. This kind of code works well. But bevor this, i use Falling edge instead of the LOW level in the external interrupt. There the problem appears that when i want to use the funktion drive() again it acts like there are interrupts at all pins. After that, if i call it again it acts normal.

My solution was to switch to "LOW" in the attach interrupt. So it seem to me that it doesnt correctly clear the interrupt when im using "Falling edge"

Is there a possibel explanation for this behavior?

Thanks for helping

What board are you using ?

Why all the funny business of attaching and detaching interrupts ?

i m using the Arduino Mega 2560.

i got that many ports so i use them :smiley: it s easyier to use these seperated interrupts (caused by the hardware around the board)

why shoulnt i attach and deatach these interrupts?

Why not just leave them all attached and react to the button presses with an ISR for each ? It involves no more wiring and uses the same number of pins, but the program would be a lot less complicated.

Come to that, do you actually need interrupts at all ? What is the purpose of your program ?

That might be an option. But later i want to turn them off anyway. But that discuission wasnt my real question. :slight_smile: My code is working. But i didnt get it why it does with the "LOW" Level Int, but not with the "Falling" edge.

So maybe someone sees the difference.

The solution to get the "FALLING" Int working is to remove the detachinterrupt() out of the ISR. Maybe it cancled bevor something comparing in the chip could be resetted to compare a "new falling edge".

XD

Crazy, but that might be a explanation...

Interrupts are automatically disabled whilst in an ISR, so who knows what the effect of detaching them from within an ISR is. If you need to detach interrupts "later" then do it when you need to, not every time the ISR is called.

As you have not posted all of your program or any inkling of what it does as a whole it is still not obvious that you need to use interrupts at all.

if (x<4)

Inside the for loop that iterates for x from 0 to 3, how can x not be less then 4?

    switch(x)
    {    
    case 0:     //       
    interruptNr = 1;
    break;
    case 1:     //       
    interruptNr = 0;
    break;
    case 2:     //       
    interruptNr = 5;
    break;
    case 3:     //       
    interruptNr = 4;
    break;
    case 4:     //       
    interruptNr = 3;
    break;
    }

A lookup table would be far simpler to understand/maintain:

interruptNr = intNum[x];

where

byte intNum[] = { 1, 0, 5, 4, 3 };

is define as a global array.

There is no reason to go out of your way to write verbose/convoluted code when there are far simpler ways of achieving the needed results.