Is there any way to control external interrupts?

Hi friends, i want to know, is there any way to control any interrupt pin in this way :
if(something is true)
{
let external interrupt pin (for example INT0 pin) react to actions
}

else
{
does not matter whatever happens, dont let external pin to react anything
}

i am using arduino mega, and planning use all 7 interrupt pins, even PCINT pins, but i dont need my external interrupt pins react everytime to everything.

so i cant use cli(); because it will disable all interrupts

Set a flag indicating that the ISR should do nothing and test the flag in the ISR. If the flag is set then return immediately

Could you attach a dummy interrupt handler?

you mean something like this?

bool flag = false;
ISR(INT0_vect)
{
  if(flag == true)
  {
    Serial.println("something");
  }
}
void setup() {
  Serial.begin(9600);
  DDRD &= ~(1<<PIND2);
  PORTD |= (1<<PIND2);
  EICRA &= ~(1<<ISC01);
  EICRA |= (1<<ICS00);
  EIMSK |= (1<<INT0);
  sei();
  
}

void loop() {
 for(int i = 0 ; i<100; i++)
 {
  if(i == 15){flag = true}
  else{flag = false;} 
 }
delay(1000);
}

No, because, of course, you'd never do serial I/O in interrupt context.

i started to do some research about "dummy interrupt handler". never met before. seems like something helpful

That is the sort of thing that I had in mind.

I realise that your code was only an example but do not expect Serial.print() to work reliably in an ISR because it uses interrupts and they are disabled when in an ISR. I seem to remember some talk of a proposal to change this but I am not sure whether it was ever implemented

One question that does arise is whether you need to use interrupts in the first place. What is the application ?

my application is about controlling ac motor speed with zero cross detection circuit. there are some other menu functions, screen, sensors, buttons and etc. actually i cant post the code because it is very long and complicated.
and i want my INT0 pin react to zero cross detection optocoupler only when i decide from menu.

Do you really need to use 7 interrupts as you say in your original post ?

Oh yes you can, but use code tags when you do to make it easy to read and copy for examination

will it work this way?

bool flag = false;
ISR(INT0_vect)
{
digitalWrite(5 , HIGH);
}
void setup() {
 pinMode(5 , OUTPUT);
  DDRD &= ~(1<<PIND2);
  PORTD |= (1<<PIND2);
  EICRA &= ~(1<<ISC01);
  EICRA |= (1<<ISC00);
  EIMSK |= (1<<INT0);
  sei();
  
}

void loop() {
 for(int i = 0 ; i<100; i++)
 {
  if(i == 15){EIMSK &= ~(1<<INT0);}
  else{EIMSK |= (1<<INT0);} 
 }
delay(1000);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.