[Solved] Getting a button to run a function

I’m trying to get a simple button to run a function.

Hardware: Particle Photon

I have other values flowing from like temp and humidity and that is showing up just fine in Blynk. I have no delays in the loop and actual did a loop count to verify that it is looping around 1000 a second.

The button is configured as “push” on V2.

Can anybody see what I’m doing wrong or is there a better way to handle this?

note: I cut out a lot of the other code that checks on the status of other pins and calls to particle’s web hooks but this should be the core of the logic to run a function.

int toggle_from_blynk = 0;

void setup(){



void loop(){

      A lot of other stuff runs here

      toggle_from_blynk = 0;
       /* This will be the call to the function */

int toggleRelay(String command){


    Particle.publish("Toggled", 60, PRIVATE);

    return 1;

   toggle_from_blynk = 1;

I’m using a button in a similar fashion in this manner:

  int pinData = param.asInt();

  if (pinData == 0)

With push button on click hardware gets 2 BLYNK_WRITE(V2) invocations. Correct approach would be as @structure7 suggested.

Ok, I understand that the suggestion above would prevent my original code from executing twice but I’m having trouble getting my BLYNK_WRITE to execute even once. If I tap on the button enough times in my app, like 10 times, it will eventually fire the code once.

@tyler785 what about simplest possible sketch? Just 1 button in app and 1 handler in sketch. Does it work fine?

Do you ever get a Particle.publish without a relay trigger, or vice versa?

You can toss #define BLYNK_DEBUG into the top of your code and confirm the BLYNK_WRITE is making it in. I usually prefer to place a Serial.println here and there and watch the console to make sure things are happening.

@Dmitriy and @structure7, Sorry its been so long but I solved this issue. This was actually being caused because I was flooding the service so the button was not functioning properly. I fixed the flood and the button is working perfectly! Thanks for the support and patience!

1 Like

All’s well that ends well! :grin:

A post was split to a new topic: Keep getting the error in compiling…