Go Down

Topic: Hardware interrupt problem (Read 327 times) previous topic - next topic

mmurphy

I am having trouble with a hardware interrupt.
It is set to pin2 and does activate when pressed. I want it to run a little subroutine and it does that too.
It then return to the main loop where it left off and that's waht I don't want to happen.
I don't want it to excecute the main loop after the interrupt and just sit idle.

dxw00d

Do you need to declare runstate as volatile?

PaulS

Quote
It then return to the main loop where it left off and that's waht I don't want to happen.

That's the nature of an interrupt. Stop what you are doing, complete this higher priority task, then resume what you were doing.

Quote
I don't want it to excecute the main loop after the interrupt and just sit idle.

You don't have any choice about where to resume. You do have a choice whether loop() recognizes that the interrupt occurred, and modifies its behavior based on whether or not the interrupt occurred.

kf2qd

So - you just want this code to execute when the interrupt occurs?

You have 2 choices -
either put the code in the interrupt routine and it will execute whenever the interrupt is received

or

In the interrupt routine set a flag that you reset inside your routine in loop.

Kind of like this -

void Loop(){

    if (Flag) {
         your code here...
         Flag = 0;
      }

void Interrupt (){
   Flag = -1;
}

Working from my old and tired memory - you might want to swap the Flag = 0 and Flag = -1 to have it work properly.

Warning - will the code you want to run every interrupt execute in less time than the time between interrupts?

Go Up