why my interrupt INT0 triggers the routine for INT1 [solved]

// Definition of interrupt names
#include <avr/io.h>
// ISR interrupt service routine
#include <avr/interrupt.h>
// LED connected to digital pin 13
const int leftLEDPin = 4;
const int rightLEDPin = 5;
// This is the INT0 Pin of the ATMega8
const int lSensePin = 2;
const int rSensePin = 3;
// We need to declare the data exchange
// variable to be volatile - the value is
// read from memory.
volatile int leftIndicatorState = LOW; //if the left indicator is on and led is flashing

volatile int rightIndicatorState = LOW; //if the left indicator is on and led is flashing

// Install the interrupt routine.
ISR(INT0_vect) {
  // check the value again - since it takes some time to
  // activate the interrupt routine, we get a clear signal.

  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 200ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 200) 
  {
      leftIndicatorState = !leftIndicatorState;
      Serial.println("hmm interesting");
  }
  last_interrupt_time = interrupt_time;
   
}
//routine for interrupt 2
ISR(INT1_vect) {
  // check the value again - since it takes some time to
  // activate the interrupt routine, we get a clear signal.

  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 200ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 200) 
  {
      rightIndicatorState = !rightIndicatorState;
      Serial.println("right interrupt triggered");
  }
  last_interrupt_time = interrupt_time;
   
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing ihandler");
  // sets the digital pin as output
  pinMode(leftLEDPin, OUTPUT);
  // read from the sense pin
  pinMode(lSensePin, INPUT);
  pinMode(rSensePin, INPUT);
    
  Serial.println("Processing initialization");
  // Global Enable INT0 interrupt
  GICR |= ( 1 << INT0);
  GICR |= ( 1 << INT1);
  //falling edge triggers interrupt0
  MCUCR |= ( 0 << ISC00);
  MCUCR |= ( 1 << ISC01);
  //falling edge interrupt 1
  MCUCR |= ( 0 << ISC10);
  MCUCR |= ( 1 << ISC11);
    
  Serial.println("Finished initialization");
}

void flashLed(int pin, int state)
{
   if(state){ 
      digitalWrite(pin,HIGH);
      delay(300);
      digitalWrite(pin,LOW);      
   }
  else{
      digitalWrite(pin,LOW);         
  } 
}

void loop() {
   //actually flash the led if the indicator is on
   flashLed(leftLEDPin,leftIndicatorState);
   flashLed(rightLEDPin,rightIndicatorState);
  
  //run chevrons animation  
  
  delay(100);
}

This is a project am doing for my local fabrication lab. The problem is despite the routine names are clearly different, INT0 seems to call INT1 as well as it’s own routine and vica versa.
I would appreciate if someone could proof read the code to check it’s not the code.

What does your hardware look like? Got pullup resistors?

What is your objection to using attachInterrupt?

And what tmd3 said.

I did try attacheInterrupt but had the same problem.

^5v INT0 | | | | | | ----------10k pull up ---------switch --------- GND

Similar setup for INT1 I just unplug the wire then connect it to digital pin 3 instead of 2

Why are you saying it could be debouncing issue?

Because of cost issues am using a atmega 8 with the atmega8 NG or older option within the boards menu in the IDE.

If both interrupts are enabled, and you only have a pullup resistor on one of the two (you said you just swapped it between the pins), then one of the interrupt pins will always be floating and random noise could trigger the ISR. If you haven't already done so, you could try changing this:

  pinMode(lSensePin, INPUT);
  pinMode(rSensePin, INPUT);

To this:

  pinMode(lSensePin, INPUT_PULLUP);
  pinMode(rSensePin, INPUT_PULLUP);

To ensure both always have a pullup resistor.

If the signal wires / traces for INT0 and INT1 run any distance together (more than a few inches) then cross talk might be the reason. You'd need much stiffer pull ups to prevent that (330 ohm??) or more simply add some 100pF caps to ground at each input to slow the edges down and smear out the crosstalk spikes.

I'd have thought that if you are interfacing two push buttons you'd need software debouncing anyway which will DTRT and you won't need to worry about this happening as it will be treated as a bounce?

Tom you're right of course. I did relise my rookie mistake as I went to bed. This is what you get for pulling a all nighter the previous night. I'm very tired. My mate says I look ill. lol. HAHA am sorry for wasting your time guys. Thank you for helping me anyway.

I faced the same issue. In my setup, two push buttons are connected to Arduino Uno pins 2 and 3 as explained at https://www.arduino.cc/en/Tutorial/Button (with 10kOhm pull down resistors).

The board is powered either from USB, or from a 9V battery. The behavior was the same.

Issue: When only a single one of the buttons is pressed, the serial monitor shows that both int0 and int1 are triggered.

I am aware that Serial should not appear in an ISR, but this was just for the issue demonstration.

void ISR_INT0_vect() { Serial.println("int0"); }
void ISR_INT1_vect() { Serial.println("int1"); }
void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT); pinMode(3, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), ISR_INT0_vect, RISING);
  attachInterrupt(digitalPinToInterrupt(3), ISR_INT1_vect, RISING);
}
void loop() {}

Thanks to MarkT, I solved the issue. The problem was caused by the "cross talk" between the two signal wires, which ran together for about 50cm (about 20 inches). As soon as I split the two signal wires apart, the issue disappeared. I will consider using the capacitors as suggested. Thanks!

The correct fix is not just to separate the wires, but also provide each with a ground return wire next to it. If you use ribbon cable to send signals a good approach is make every 2nd or every 3rd wire ground, so that every signal is next to a ground return. This greatly reduces the magnetic pickup to the wire from stray fields from high current circuitry.

With CAT5 cable each signal nees to be individually paired with ground in a separate twisted pair. You can use Vcc instead of ground as a return if you wish.