AttachInterrupt Problem

Hi,

I’m attaching an interrupt to a port with a PIR sensor (see code below). But nothing happens when the PIR sensor is “activated” or “de-activated”. Removing pinMode(pir, INPUT_PULLUP); doesn’t change anything. What am I missing here?

indent preformatted text by 4 spaces

Thanks.
Guy


    int pir = D0;
    int blueLED = D1;
    
        indent preformatted text by 4 spaces
    
    void pirOn(void);
    void pirOff(void);
    volatile int state = LOW;
    
    void setup() { 
        pinMode(pir, INPUT_PULLUP);
        pinMode(blueLED, OUTPUT);
        attachInterrupt(pir, pirOn, RISING);
        attachInterrupt(pir, pirOff, FALLING);
    }
    
    void loop() {
     	// These statements work
        /*if (digitalRead(pir) == HIGH) {
            digitalWrite(blueLED, HIGH);
        } else if (digitalRead(pir) == LOW) {
            digitalWrite(blueLED, LOW);
        }*/
        
        digitalWrite(blueLED, state);
    }
    
    void pirOn() {
        // Duplicate for testing
        digitalWrite(blueLED, HIGH);
    	
        state = HIGH;
    }
    
    void pirOff() {
    	// Duplicate for testing
        digitalWrite(blueLED, LOW);
    	
        state = LOW;
    }

@gdillen, you can only associate a single ISR to a pin. In your case you could use CHANGE instead of FALLING and RISING then in the (single) ISR read the pin to decide what the change was ( LOW for FALLING and HIGH for RISING). :smile:

2 Likes

@peekay123 Thanks. I thought wrongly:

  • I could attach 2 interrupts to the same pin.
1 Like