Toggle LED with IFTTT DO

Hello Community

I am trying to simply toggle the Status of a pin (D7) via a IFTTT DO Button.

I can call either a function directly on the Photon or publish an event which the Photon will listen to.

However i would prefer an Option where i can call a function directly.

Press the IFTTT DO Button once -> D7,HIGH (LED on) -> Press the IFTTT DO Button again -> D7,LOW (LED off) -> and so on

I am just overwhelmed by the Information available, but especially the Toggle Aspect seems to be complicated as i found multiple solutions online. Can Anyone help?

Best Regards =)

Hey there, welcome to the community!

Toggling an LED shouldn’t be too hard. The easiest way is by checking the current state of the pin, and then inversing that. I’ve written a quick (untested!) demo in <30 seconds, which you could give a try.

int ledPin = D7;

void setup() {
    pinMode(ledPin, OUTPUT);
    Particle.function("togglePin", togglePinFunction);
}

void loop() {

}

int togglePinFunction(String command) {
    digitalWrite(ledPin, !digitalRead(ledPin));
    return 1;
}

Let me know if that worked :slight_smile:

1 Like

It worked!! =)

1 Like

Just a common coding tip:
A nice behaviour of any Particle.function() would be to return a useful value.

In this case the current state might be more useful :wink:

int togglePinFunction(String command) {
    int state = !digitalRead(ledPin);
    digitalWrite(ledPin, state);
    return state;
}
2 Likes

Nobody asked for it to be useful :stuck_out_tongue:
Considering it’s regarding IFTTT DO, there’s very little use for a useful return, but yes, your approach is generally more elegant :wink:

But then you might also argue that you shouldn’t actually do stuff in a function call, other than setting a flag and reacting to it in the loop :innocent:

3 Likes

Actually I didn’t intend to reply to you, since I know you know, but just wanted to add that point for Paul :blush:

(CleverClogs has spoken - haw :hand: )

3 Likes

Thank you both verry much :slight_smile:

3 Likes