ISR(INT0_vect) {
if (key_pressed==0) { //now, key is pressed
key_pressed = 1; //set the flag
//now active isr on the rising edge, to detect the release of the key
EICRA = (EICRA & ~0x03) | //preserve eicra bit 7..2
(1<<ISC01) | (1<<ISC00) //interrupt on int0's rising edge
;
} else { //now, the key is released
key_pressed = 0; //reset the flag
EICRA = (EICRA & ~0x03) | //preserve eicra bit 7..2
(1<<ISC01) | (0<<ISC00) //interrupt on int0's falling edge
;
}
}
This is far too complex in my opinion. There is already a perfectly easy to use system in the Arduino library: attachInterrupt. If you do an attachInterrupt on a CHANGE interrupt all you have to do is test the key state in the ISR.
This register-manipulation is very processor-specific and not easy to understand.
I have example code on my page here:
I hope you already looked at it. The first example shows detecting a switch press:
const byte LED = 13;
const byte BUTTON = 2;
// Interrupt Service Routine (ISR)
void pinChange ()
{
if (digitalRead (BUTTON) == HIGH)
digitalWrite (LED, HIGH);
else
digitalWrite (LED, LOW);
} // end of pinChange
void setup ()
{
pinMode (LED, OUTPUT); // so we can update the LED
digitalWrite (BUTTON, HIGH); // internal pull-up resistor
attachInterrupt (0, pinChange, CHANGE); // attach interrupt handler
} // end of setup
void loop ()
{
// loop doing nothing
}
The "change" interrupt can respond to switch-on or switch-off. In this case it finds the current state and flashes an LED. You can change that part to do what you want.
@dhenry: I do not agree with using low-level registers when someone has taken the trouble to write a library to simplify things for you, unless you can demonstrate a clear advantage in a particular case.