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
Victor.