Stepper Motor problem with Interrupt Function

Hello everyone, I'm new at the forum and I need some help as I can't figure out what's wrong...

So I'm making a program and I need to move a Stepper motor a certain quantity of stepps everytime there is a LOW TO HIGH change on a digital signal from a tacometer (to synchronise a process with another motor). I made some other programms WITHOUT interrupts and had no problem with them as the motor did exactly what I programmed, but then when I added the function to move it INSIDE an interrupt function it "works" (as it just makes a little noise movement only once) but then it doesn't responds, but whenever I delete this motor function from inside the interrupt everything works fine;

The interrupt function:

void tacometro(){

  myStepper.step(40);
  mt++; //variable that I show on an LCD
  digitalWrite(ledTaco,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Metros:");
  lcd.print(mt);
  //ACA VA LA SECUENCIA PARA EL MOTOR DE PASOS, QUE DEPENDE DE CUANTOS PULSOS LLEGARAN en 1 vuelta
  }

My setup code:

void setup() {

  myStepper.setSpeed(100);
  pinMode(ledMotor,OUTPUT); //Led indicador de motor on en pin 2
  pinMode(ledTaco,OUTPUT); //Led indicador de pulso en el tacómetro

  pinMode(interruptPin,INPUT_PULLUP);

  pinMode(en1,OUTPUT);
  pinMode(en2,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(b1,OUTPUT);
  pinMode(b2,OUTPUT);  

  detachInterrupt(digitalPinToInterrupt(interruptPin)); //Asegura que no habrá interrupcion sin que entre al programa del motor

  lcd.begin(numCols,numRows); //Se inicializa LCD de 16x2
  lcd.clear();  //Se limpia el LCD antes de iniciar el loop
}

I'm using the detach Interrupt at the beggining so there is no movement of the motor when it shouldn't be, it gets activated at reaching this another funtion:

void motor (){
  
  attachInterrupt(digitalPinToInterrupt(interruptPin),tacometro,RISING);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Iniciando");

  digitalWrite(ledMotor,HIGH); //Enciendo el led de motor en ON
  delay(2000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Metros:");
  lcd.print(mt);
  
  analogWrite(pinPwm,0); //PWM apagado en el pin pwm 2

  partida();

Those are the only parts of the code where the stepper motor takes place...

I've tried changing the code many times and the only solution for the interrupt to work ok is to take off the myStepper.step(40); line, it doesen't matter where I put the interrupt function, it just won't work.

Help please! I couldn't find anything :frowning:

Victor.

Of course it won't work. Did you ever read the documentation on the interrupt function? Did it say anything about how few instructions should be in the interrupt function? What do you have?

Paul

I found that the delay() function can't be used, and I assume that it is used inside the step() function of the motor... I didn't read it before. So now I tried calling another function from inside the interrupt to use the step function and didn't work as well... any ideas?... I'm not too much into programming to be honest and this got a little difficult to me... thanks for your reply!

Look at ALL the time consuming things you are doing in the interrupt function. Doesn't matter if you use another function to do them. An interrupt function should just set a boolean to indicate there was an interrupt and then in your main program loop check the boolean for "TRUE" and do something there because of the interrupt. eventually set the boolean back to "FALSE".

Nothing else can happen in your program until the interrupt code has completed.

Paul

@Vitocolr, you should not be trying to run the motor while in the Interrupt Service Routine (ISR)

Your ISR should be something like this

void tacometro(){
   pulseDetected = true;
}

and your normal code should contain code like this

if (pulseDetected == true) {
   pulseDetected = false;
   myStepper.step(40);
   mt++; //variable that I show on an LCD
   digitalWrite(ledTaco,HIGH);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Metros:");
   lcd.print(mt);
 
 }

You need to post the complete program as a single piece. I don't understand why you don't have the attachInterrupt() in setup() but it might be obvious when I see the whole program

...R