attachInterrupt Question

Hi Guys,

I was wondering if it's possible to refer to an interrupt process in an if statement?

for example:

If (interrupt hasn't been called) in more than 2500 millis; THEN variable = 0

I'm not exactly sure how I'd write this code, any help would be appreciated.

You need to set up a variable or variables to use to communicate between the interrupt routine and the rest of your code.

These are declared "volatile" in order that the compiler knows they can change under its feet.

volatile int  interrupt_count = 0 ;
int last_count = 0 ;

void int_routine ()  // interrupt handler
{
  interrupt_count ++ ;
  ..  // any other stuff it does
}

void setup ()
{
  ..
  attachInterrupt (0, int_routine, RISING) ; // or whatever
  ..
}

void loop ()
{
  if (interrupt_count - last_count > 0)
  {
    last_count = interrupt_count ;
    ..  // here we know it has been called since last time we checked
  }
  ..  // other stuff
}

[ this isn't quite correct though - int variables take two instructions to update on an 8-bit microcontroller, so the loop() code might see it after only half has changed - you need to look at sei() and cli() in the documentation to see how to temporarily disable interrupts so such multi-byte volatile variables can be read without error ]