[SOLVED] Multiple interrupts monitoring CHANGE


#1

I’m hoping to use my bluz hidden inside a wall lamp switch to detect when the switch is flipped. The switch is double as it controls 2 sides of the lighting, so I wanted to attach 2 CHANGE interrupts. I didn’t find any info to why this shouldn’t work, but no matter what I try, only one of them works.

int btnL = D0;
int btnR = D2;
volatile bool switchFlipL = false;
volatile bool switchFlipR = false;

void setup() {
    pinMode(btnL, INPUT_PULLUP);
    attachInterrupt(btnL, onBtnFlipL, CHANGE);
    pinMode(btnR, INPUT_PULLUP);
    attachInterrupt(btnR, onBtnFlipR, CHANGE);
  
    RGB.control(true);
    RGB.color(0,0,0);
}

void onBtnFlipL() {
    switchFlipL = true;
    RGB.color(0,255,0);
}

void onBtnFlipR() {
    switchFlipR = true;
    RGB.color(0,0,255);
}

void loop() {
    if (switchFlipL){
        if (Particle.connected()) {
            Particle.publish("switch-flipped", "left", 60, PRIVATE);
        
            switchFlipL = false;
            RGB.color(0,0,0);
        }
    }

    if (switchFlipR){
        if (Particle.connected()) {
            Particle.publish("switch-flipped", "right", 60, PRIVATE);
        
            switchFlipR = false;
            RGB.color(0,0,0);
        }
    }

    System.sleep(SLEEP_MODE_CPU);
}

Am I doing something wrong here, or is this technically not possible?


#2

That is definitely possible, your code should work fine.

What version of firmware are you using? There was a bug in v2.0.50-beta.2 where D0 and D1 weren’t working. This has been fixed in beta.4 or you can put the line:

Wire.end()

at the beginning of your setup function. If the interrupt on D0 is the one not working, and you are on v2.0.50-beta.2 that is probably the cause.

If you aren’t using that version, which one works and which one doesn’t?


#3

Hi Eric & thanks for your time!

So both work separately, but only D0 works when both enabled.
I’m using the default 1.1.47.


#4

Can you try updating to v2.0.50-beta.4? It is in the Web IDE and is very stable.

I just noticed that we did indeed fix this after 1.1.47: https://github.com/bluzDK/bluzDK-firmware/commit/aaeb9ec39aa274bf65efe3cccbb775022ca8a2d9

So the number of interrupts would only be 1 in 1.1.47 and it is now 6.


#5

Yes! That fixed it! :slight_smile:
Thanks!