Run motor on interrupt

Hello Arduino forum.
I am trying to run a motor when my IR sensor is RISING, my interrupt works, but when i add the motor to the interrupt then it does not work. My motors does work outside the code and the interrupt prints to serial when I comment out the motor.

I dont really understand the problem, could someone tell me why it doesn't work?

//libraries
#include <TimerOne.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

//Ports assigned
int IR_2 = 0; //IR sensor on port D2
int IR_3 = 1; //IR sensor on port D3
int IR_4 = 2; //IR sensor on port D4
int IR_5 = 3; //IR sensor on port D5
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motor1 = AFMS.getMotor(1); //motor 1 on port M1
Adafruit_DCMotor *motor2 = AFMS.getMotor(2); //motor 2 on port M2

void setup() 
{
  AFMS.begin();
  motor1->setSpeed(100); 
  motor2->setSpeed(100); 
  Serial.begin(9600);
} 
void test(){
 int ir2 = digitalRead(IR_2);
 Serial.print(ir2);
 motor1->run(FORWARD);
 delay(1000);
 motor1->run(RELEASE);
}


void loop(){
 attachInterrupt(IR_2, test, RISING);
 

}

Serial I/O and a delay in an ISR?
Seriously?

Well I wouldn't have serial.print in there. That was just for test, and yes it is maybe really bad what I am doing, but I am totally noob.
But why doesn't it work with running a motor?

You seem to have your entire concept back to front.

You should attach the interrupt in setup().

loop() runs at thousands of times per second - why would you want to attach the interrupt that often?

All that should be in the Interrupt Service Routine (ISR) is one line that sets a variable to say the interrupt has happened.

Then the code in loop() should check that variable and start the motor if the interrupt has happened.

Now we get to the interesting piece ...

How often does the interrupt happen?
How long will the motor run after each interrupt?

I find it very hard to reconcile the need for interrupts which detect things at the microsecond level with running motors which usually operate for several seconds.

It would be a good idea if you describe your overall project without any assumptions about how it is to be implemented.

...R

There wasn't anything specific i was trying to do with this code. I was just testing stuff and trying to understand how it works.

Makes sense to set the interrupt in setup(), I think I had just misunderstood the concept of ISR.

I understand now how it works, thanks for the help.
appreciate it a lot

And you can't use delay in the ISR. It will just lock up your code!. the use of serial tx (print/write etc) will also cause problems.

Mark