Pin change interrupt occur when enabling it back

I'm new to Arduino.

I want to use pin change interrupt PCINT0.
I connected a mechanical switch to PCINT 3.

the switch has no filtering so as I get into the ISR(PCINT0_vect) i disable PCINT0 (tried also only to disble PCINT 3).

here is the code:
//Pin change interrupt
/* interrupt code here */
PCICR = 0; //disable pin change interrupts
PCMSK0 &= 0xF7; //set bit 3 to 0 mask PCINT 3 - Disable PCINT 3

Serial.print("PCIFR ");Serial.print(PCIFR);Serial.println();

//Serial.print("PINB "); Serial.print(PINB); //Read port values for test only
//digitalWrite(ledGreen,!digitalRead(ledGreen)); //for test only


and it works.
the problem is when i re-enable it after few seconds some times the interrupt occur again, only one after enabling it without touching the switch.
I also see that PCIFR = 0 when it enters the ISR.

if then I will enable it again and will not touch the switch anymore the interrupt will not occur.

I switch input is configured to use the internal pull-up.

Any Idea?

Hi, welcome to the forum.

It is not allowed to use Serial.print (or any other Serial function) inside an ISR. That might have strange results, like you have.
Changing the interrupt bits inside an ISR is dangerous of course. I can not tell if that will work, I would have to read large parts of the datasheet about all the interrupt flags.

Could you have a broader view at your project ? Why do you want to read a switch that way. Could you use polling, or perhaps the debounce library.
Interrupts are great for communication with other devices, or interrupts from sensors, or special hardware. But a simple button doesn't require that. At least, I never had to use an interrupt for a simple switch or button.

the it worked correctly after improvising an RC filter.

I took your advice and i'm doing pooling with debounce.


cool 8)

Note that serial.print should never be used within an interrupt routine as it is a blocking function, so random problems may still occur.