Using While or do...While to clear a hard interrupt

This may be an easy question, but I have a linear actuator moving and when it activates a limit switch, I have programmed a hard interrupt to stop movement in that direction. Here is the first part of my question, while this interrupt is enabled (limit switch is active), am I correct in stating that I will not be able to move the linear actuator at all, say if a push a button to move the linear actuator in the opposite direction? The second part of my question is upon activating the limit switch, I was thinking of programming it to move in the opposite direction while the switch is activated and was thinking of a type of while loop, would it be better to use a do...While or just a While?
Thanks

  • Always write non-blocking code.

  • Not recommended to use interrupts with switches; just scan them every ~20ms looking for a change in state.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.

1 Like

If you must use an interrupt but I doubt it simply set a flag then process the flag in your main code. In your case you want the interrupt to be edge triggered so you do not have to wait until it clears.

We really need to see a wiring diagram and source code in code tags.

Is your limit switch physically cutting power to the motor? If so, then you are correct. If the limit switch is just an input to the Arduino, you can make it do whatever you want.

Interrupts are normally triggered on a signal edge (rising or falling) not a signal state (high or low).

As mentioned by @LarryD this is not a good use of an interrupt.

Neither, that would be blocking code. The Arduino would be able to do nothing else because it would be stuck in that while/do-while loop. Better to set the motor in motion, take a record of the time (millis()) and let loop() continue to execute, until the desired motor running time is up, or some other event occurs.

that depends on what your interrupt code does. it would make sense for the interrupt service routine (ISR) to invoke code that only stops the actuator when moving in one direction.

pressing a button that invokes code to move the acutator in the opposite direction should not be affected by an interrupt written as descibed above

the same would be true using if statement in loop() instead of an ISR to monitor a limit switch

    if (Active == digitalRead (PinLimitSw) && Extend == dir)
         stop ();

Thanks to everyone that has commented. Just for clarification, I am using this interrupt similar to an E-stop. This is to prevent the linear actuator driving a pusher rod into the end of a cylinder (i.e.; Empty Cylinder indication). This interrupt is an input into the Arduino and does not cut the power to the motor directly. I was asking this question to get an opinion on how to clear this interrupt ( I have not finished coding this yet and have not created a schematic yet).
As I was writing a reply to this, could someone explain (other than the obvious reasons) why using a hard interrupt? I do understand the reason for writing non-blocking code, but in this case, I want it to, so that the operator has to take action to clear it.

Again, thanks for all the responses.

In certain situations it's totally ok to use blocking code, just be aware of it.
While is checking first the condition and if true, runs the loop.
Do While runs first the loop once and then checks the condition.

Don't confuse process blocking with blocking code. A process can very well be blocked (waiting for an external action) while still being implemented with non-blocking code. And with fast non-blocking code, the use of interrupts can be avoided.

this is confusing ... an interrup is typically used to avoid interfering with the foreground code?

is the foreground code "blocking"?

if foregound code detected the limit switch, or recognized some other event and stopped some activity, wouldn't the operator need to take some action?

it's not really clear what you trying do either in the code or as a learning exercise