Interrupting an interrupt

Hello! I’m new to this…

I need help with interruptions. What I want to do seems to be very easy. I am making a Dip Coater…

My problem is that I need to send my stepper to a “Home” position, and as my system has no feedback… My arduino is not able to know where my stepper stopped the last time.

Im trying to do this: (this is no my real code, im testing with this one)

With interruptions, I press a push button called “casa” to move to Home position (backhome) BUT!!!
I don’t know hoy many steps does it takes to get there!!!

So… I put another pushbutton called “senal” (Limit Switch)… and I added an Interrupt… My idea was that when my LimitSwitch called “senal” got pushed… it would stop backhome()… but it’s not working, my stepper keeps going the qty of steps I typed.

SO… my question is… How can I stop and Interrupt?

THANKS IN ADVICE! :sweat_smile:

#define DIR_PIN   7 //  CW+     Driver del motor.
#define STEP_PIN  9 //  CK+     Driver del motor.
#define EN_PIN    5 //  EN+      Driver del motor.

int a=2;
int b=3;
float casa; // pushbotton to send backhome
float senal; // LimitSwitch

void setup() 
  pinMode(DIR_PIN, OUTPUT);   // Declara como salida para el driver - motor
  pinMode(STEP_PIN, OUTPUT);  // Declara como salida para el driver - motor
  pinMode(EN_PIN, OUTPUT);    // Declara como salida para el driver - motor
  digitalWrite(EN_PIN, HIGH);  // Prende el driver

  attachInterrupt(0, paro, CHANGE);
  attachInterrupt(1, backhome, CHANGE);

void loop()
senal = digitalRead(a);
casa = digitalRead(b);

void paro()
  // do nothing, I only want to stop my backhome()

void backhome() 
  motor(10000,0.3); // Moves to the top or home position

void motor(int steps, float speed)
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);


  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++)
    digitalWrite(STEP_PIN, HIGH); 

    digitalWrite(STEP_PIN, LOW); 

Cant you calculate how many steps you have made and revers those to go back home? Like increment a variable everytime to do a step with the motor. Just an idea.

Sounds like a good idea, actually

but, what if my stepper loses steps?

Well, it's been happening to mine and since I haven't found the stepper I need, I need to continue with this one by now. :disappointed_relieved:


This is an age-old problem! Old hard disks had this problem, they would not know for sure where the stepper was positioned. Early on, the microcontroller would just force the actuator arm against a rubber stop by issuing more than the total number of steps! The drives sounded like little automatic weapons. Drives today generally have feedback to assist with position synchronization.

You need at least one limit switch, or if your stepper has stops you can do the count past limit trick. If you use a switch, you should only need to initialize the stepper once to 0 count during setup(); there after just incrementing or decrementing counts. If your stepper is loosing position, that issue should be corrected - perhaps replacing that unit with a more capable unit or double checking to ensure the power supply is providing the correct voltage and supplying enough current.

If you still want to implement an interrupt, this may be helpful:


The way you are using your interrupt is bad. Your approach requires far too much to happen in the ISR - and what happens won’t finish quickly so there could be several interrupt calls to the same routine before the motor finishes its first move. Guaranteed confusion, if nothing worse.

The simplest way is for the ISR to set a global variable (eg. homeButton = true;) to denote the fact that the interrupt had been triggered. Code elsewhere would then cause the motor to move or not depending on the value of the variable, and would clear the variable when the move was complete.

HOWEVER … it is extremely unlikely that you need to use interrupts just to detect switches that control motors. Your code should be designed so that loop() repeats many times per second and can check the switches on each iteration.


I agree - there is no need to use interrupts at all for this problem, let alone nested interrupts. Using interrupts introduces considerable design complexity and requires particular coding techniques to work correctly. They should only be used when necessary, and then only reluctantly and to the minimum extent necessary to solve the problem.

Yeah, thanks!

it was true, there was no point in using interruptions for this code and actually everything about this is going perfectly.

Once you said that I started looking for other ways... and didnt lose time on it anymore :smiley: thanks