Tying to trigger a function on photon and failing

I am attempting to trigger a simple event on my photon via IFFFT. If a new email is received then blink all the leds 3 times. I’ve scoured the boards and found some good code examples but can’t seem to find a breakthrough. I am a complete n00b to this so apologies for stupid mistakes. Any help greatly appretiated

Here is my code:

#include "InternetButton/InternetButton.h"
InternetButton b = InternetButton();

int count = 0;

bool toggle = false;

void setup() {
    b.begin();
    Particle.function("notify", ledToggle);
}

void loop() {
    if (toggle == true){
        
        if(count<3) {
        b.allLedsOn(0,20,0);
        delay(500);
        b.allLedsOff();
        delay(500);
        count = count + 1;
        }
        
        toggle = false;
    }
}

int ledToggle(String command){
    toggle = true;
}

And the screen grab for IFTTT:

It doesn’t by any chance work a single time, does it?
In any case, you should take a look at your Count variable. thats a global any, and thus accessible from anywhere. In your loop, you check if it’s smaller than 3, and if so, blink and increase. That should work fine, were it not for the fact that you never reset the count. It thus then holds 3, and wont allow your IF block to run again. You could set it to 0 again, just like you reset your toggle variable. that should work.

Alternatively, use this syntax for the loop:

#include "InternetButton/InternetButton.h"
InternetButton b = InternetButton();

volatile bool toggle = false;

void setup() {
    b.begin();
    Particle.function("notify", ledToggle);
}

void loop() {
    if (toggle == true){
        
        for (int count = 0; count < 3; count++) {
        b.allLedsOn(0,20,0);
        delay(500);
        b.allLedsOff();
        delay(500);
        }
        toggle = false;
    }
}

int ledToggle(String command){
    toggle = true;
    return 0;
}

I haven’t tested the above, but it should work. If you want to trigger your functions easily for testing, you could use this page. That way you don’t have to send an email every time.

2 Likes

Perfect! Thanks so much! Working :smile: