Attach TWO interrupts on the same pin ? (RISING and FALLING)

Hi everyone,

I am trying to do the following with my nano:

attachInterrupt(0, interrupt_push, RISING);
attachInterrupt(0, interrupt_release, FALLING);

However, it looks like only the second statement will be effective.
Is there any way to achieve this ?

In other words I want the external input to trigger two different methods depending on the RISING or FALLING status.

I tried implementing with CHANGE and a digitalRead of the interrupt pin but that was not satisfactory.

Is there any way to achieve this ?

Yes, use CHANGE.

I tried implementing with CHANGE and a digitalRead of the interrupt pin but that was not satisfactory.

In what way?

Why do you NEED to use one function for RISING and FALLING? If the change matters, use two different handlers.

Because the response to a RISING and FALLING need to be different.

I can do this by keeping a "state" boolean that changes on every CHANGE interrupt but I was hoping for a cleaner way.

Here's the code for this but still curious if a better solution exist:

boolean pushed = false;
int pin_interrupt = 2;
int pin_led = 13;

void setup() {                
    Serial.begin(9600);

    attachInterrupt(0, interrupt_handler, CHANGE);


    pinMode(pin_led, OUTPUT);  
    pinMode(pin_interrupt, INPUT);    
    
    if(digitalRead(pin_interrupt)){
      pushed = true;
      Serial.println("WARNING: switch was pushed during startup");
    } else {
     Serial.println("Switch state was normal during startup");
    }
        
}

void interrupt_handler() {  
    pushed = !pushed;
    
    if (pushed){
        // do my "button pushed" things 
    } else {
        // do my "button released" things
    }
    
}



void loop()
{
     digitalWrite(pin_led, pushed);
}

You can simply read the pin in the interrupt routine, to determine whether the change that caused the interrupt was rising or falling.

Be aware of switch bounce.

1 Like