help with external interrupts (atmega328)

****UPDATED

Thanks for helping me with interrupts, now i just need to know about using jumps in arduino:

(2) - How to jump to specific code? I am coding in C; and, if I remember correctly, in C, it is "goto;" however, when i write the code in arduino, it doesn't change the color of the word "goto" as it recognizing it as a function... i was wondering, maybe it will still execute when it's actually running? or does arduino have a different command to execute jumps?



hello ladies & gents,
I am currently working on making a clock/stopwatch using the ATMEGA 328...
I think that I have most of my code down, but I need to know:

(1) - FLAGGING: how to have the board determine where an interrupt has come from... if i have 2 external interrupts INT0 & INT1... Button 1, 2 (let's say they are going into the board's pins 2 and 3 - respectively)... so that i can then code execution senarios depending on interrupt location.

once i distinguish flagging, how do i code depending on which interrupt occured? are the flags valued 0 & 1? - where i can test values (i.e.: if FLAG0 = 1, do this ; else, do this? // and if FLAG1 = 1, do " .... else... ?")

and,
(2) - How to jump to specific code? I am coding in C; and, if I remember correctly, in C, it is "goto;" however, when i write the code in arduino, it doesn't change the color of the word "goto" as it recognizing it as a function... i was wondering, maybe it will still execute when it's actually running? or does arduino have a different command to execute jumps?

thank you,
P.

hey.

i'm still reading and researching... and i think i might know some of the answer to part 1.

is INT0 and INT1 used as the interrupt Flag?
i.e.: if pin 2 is pressed and supplies change (interrupt), is it INT0's value that gets changed from 0 to 1?

thus, then i can just test(?) - i.e.:

if (INT0)
{
do this;
INT0 = 0;
}

if (INT1)
{
do this;
INT1 = 0;
}

*** for arduino, where does the ISR's go? void setup (), void main/loop? ()

to use an interrupt you provide the name of a routine that is run whenever the interrupt occurs. If you use different routines for the two interrupts they will automatically know what started them.

This page has an explanation and example
http://arduino.cc/en/Reference/AttachInterrupt

ok, so while you’re online…

i thought i read that when you use attachInterrupt, you cannot store values and data and such…

so if upon an interrupt, i wanted to incrementally add the value of mode,
and mode is currently 1

i.e.:
void setup ()
{
attachInterrupt(0, i_Zero, RISING);
}

void loop ()
{
}

void i_Zero ()
{
mode++;
}

when i come out of “void i_Zero” will mode = 2? or will it still be 1?

when i come out of "void i_Zero" will mode = 2? or will it still be 1?

Interrupt routines are just like any other code they can access global variables to communicate with "loop" or have their own static variables to keep values between interrupts.

declare mode before the setup procedure as:
volatile int mode; //updated by interrupt routine
and it will behave as expected. the "volatile" keyword just tells the compiler that mode can be changed at any time.

thanks bill!

now i just need help with my second question...

Interrupt routines are just like any other code they can access global variables to communicate with "loop"

Not quite. The attachInterrupt example is flawed. It is necessary to protect data that is more than a single byte by disabling interrupts when the data is accessed outside the interrupt service routine.

For example, if mode is declared an int, this is how mode is safely accessed in loop...

int SaveMode;
uint8_t SaveSREG = SREG;
cli();
SaveMode = mode;
SREG = SaveSREG;

// Use SaveMode not mode

now i just need help with my second question...

I certainly can't speak for anyone else but in my case it would help if you provided more description than "jump to specific code".

Unless you're handling things like fatal error conditions, use of "goto" in 'C' usually implies you're doing it wrong or you don't understand the problem.
Do you need to use a "switch" instead?