Problem with Adafruit Motor Shield v2 and Hardware Interupt

I’m working on a robotics project and am interfacing a simple robot with two DC motors, an Adafruit motor shield v2, my Arduino Mega, and a infrared distance switch. I’m just trying to create a simple sketch that will move the robot forward unless the interrupt is triggered in which case I want the robot to stop, back up, turn, and then continue traveling forward. The interrupt is triggered by a drop from 5 to 0 volts from the infrared sensor. The function called by the interrupt simply calls another function to “avoid” the obstacle. I can easily get the motors and motor shield to function properly but as soon as I add a interrupt the robot only travels forward and does not react to the interrupt. I’ve run out of ideas as to what is the cause of the problem, hopefully I’ve just made a simple error. Any help or suggestions are appreciated!

Working code (without interrupt):

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *Left = AFMS.getMotor(1);
Adafruit_DCMotor *Right = AFMS.getMotor(2);

void setup() {
  Serial.begin(9600);
  AFMS.begin();
Left->setSpeed(150);
Right->setSpeed(150);
Left->run(RELEASE);
Right->run(RELEASE); 
}

 void loop() { 
Serial.println("Forward");  
Left->run(FORWARD);
Right->run(FORWARD);
delay(2000);
Left->run(RELEASE);
Right->run(RELEASE);   
delay(2000);     
 }
void obstacle() {
  avoid();
}
void avoid() {
  Serial.println("Obstacle Avoided");
}

Non working code (With interrupt):

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *Left = AFMS.getMotor(1);
Adafruit_DCMotor *Right = AFMS.getMotor(2);

void setup() {
  Serial.begin(9600);
 // attachInterrupt(1, obstacle, FALLING);
  AFMS.begin();
Left->setSpeed(150);
Right->setSpeed(150);
Left->run(RELEASE);
Right->run(RELEASE); 
}

 void loop() { 
Serial.println("Forward");  
Left->run(FORWARD);
Right->run(FORWARD);
delay(2000);
Left->run(RELEASE);
Right->run(RELEASE);   
delay(2000);     
 }
void obstacle() {
 avoid();
}
void avoid() {
  Serial.println("Obstacle Avoided");  //Can't access Serial object within interrupt function, that's why it calls this function
}

Maybe one of the libraries you are using needs this interrupt?

Try to change to a different interrupt, the Mega has 6 (I think) so you will also need to change pin for the interrupt.

see http://arduino.cc/en/Reference/AttachInterrupt

If all that fails, you could try to use one of the other UARTs on the mega(?) or soft serial.

oops..just reviewed your code again.

You are still calling serial within the ISR, even though you moved it to a different function.

Your best bet is to define a volatile variable as a flag and set this in the ISR. Then every pass of the main loop check if this flag is set and then call 'avoid()'

you may also want to split the 2 sec delays into a series of smaller ones and check the flag in between them, depending on how responsive you need it to be.

Don't forget to clear the flag once you have processed it :)

Ok thank you very much! I'll make those changes today and reply with results.