interrupt workflow

hello all,

i was trying to remember how interrupts work, but have forgotten and i am unable to state my question the way google can answer it for me, so maybe real people can help.

I am controlling a stepper and want to ease it in and out, so i made a array of speeds and steps to perform. but now i want to be able to make it shorter based on a external input. so a interrupt seems the way to go.

attaching an interrupt is no problem, but what happens when the attached procedure finishes. where does it continue? for example: the program is in a for loop and halfway the interrupt occurs, will it continue from where it was?

for example: the program is in a for loop and halfway the interrupt occurs, will it continue from where it was?

Yes. That's the whole point of interrupts. When an interrupt occurs, the processor saves the current program position (program counter, or PC), and jumps to the interrupt routine. When your interrupt routine returns, program execution continues where it left off.

Because the "interruption" can occur at any time, the interrupt may occur in the middle of a calculation. If the interrupt changes any values used in the calculation (usually by modifying a global variable), unpredictable results can occur. That's why you have to temporarily disable interrupts when you are working with any variables that an interrupt can change.

In general, I would advise people NOT to use interrupts with Arduinos. They work fine, but can greatly complicate a program, and make debugging much more difficult. For instance, you can't use the Serial routines to print debug statements from within an interrupt routine.

The other reason not to use interrupts is that in the vast majority of cases, the same thing can be accomplished without them. Only when response time is critical do interrupts make sense, in my humble opinion.

Regards,

-Mike

MM states the case well. Interrrupts are a powerful and useful tool, but should only be used when it is really required.

In most cases one can work around using interrupts if they understand program control structure well. Many times interrupts are used only because of a lack of experiance with other more standard control statements and methods.

Lefty

well, in my case, i use a external interrupt to keep my timing tight for a stepper driver. works like charm now and indeed the program continues where it was. i only change the value of one var in the interrupt so the loop where the program was in before, finishes early.

never forget to disable interrupts as soon as you enter a interrupt routine.

never forget to disable interrupts as soon as you enter a interrupt routine.

Actually this is done for you automatically by the processor's interrupt hardware, and the interrupts are re-enabled automatically (by software) when your ISR returns.

Lefty