Hardware interrupt problem

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.

Timer_w_int.pde (2.96 KB)

Do you need to declare runstate as volatile?

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.

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.

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?