creating a queue

Hello all, first time Blynk user here. I currently have a button on the app that is connected to a virtual pin (V1). I am have an ultrasonic sensor that raises a flag when an object in within a certain range. Everything is setup properly, with correct inputs and outputs and include files. I have tried using the ultrasonic sensor with a physical button and it works just fine. Example code:

    int buttonPressed = 1;

void loop() {
   if (ultraSonicFlag == 1) {    // When flag is raised;

     if (buttonPressed == 1) {
     //do stuff
     buttonPressed = 0;

What I want it to do: wait for the ultra sonic sensor to “see” an object and raise a flag. Then wait until the button on the Blynk app is pressed and run another if statement.

What it is doing: constantly checking if button is pressed and creating a queue (or buffer) then when the ultrasonic flag is raised it will keep running the if statement until the queue is empty. Even if I remove the object then replace it, the if statement will check the queue for the button press (even though the button hasn’t been pressed for some time)

I have tried setting buttonPressed = 0 everywhere in my code and it still keeps a queue.

@mstraubhaar with being within your ultraSonicFlag if statement you will have no access to Blynk until the flag is raised. Furthermore after the first 10s of a reboot you will be kicked off the Blynk cloud server as you will miss the heartbeat.

Not really sure what you want to do but your current code looks bad.

I’d start by just having and a call to a function in void loop.
The function being called should test if ultrasonic is in range && button is pressed.


There shouldn’t be access to the until the flag is raised for a safety precaution. Once the flag is raised the should look for the button press. It should not be looking for the button press while the flag is not raised.

In that case when the flag is raised you need to start Blynk (Blynk.begin() etc).

Should it not “look” for the button press, or should it not respond to the button press until the flag is raised? Could an IF statement with an && condition be used.

I thought the Blynk.begin(); had to be in the setup? Or can it be called in the loop(); ?

I’m not sure which would be better. Either way the button press should be ignored until the flag is raised, and then it can’t be pressed again until the flag goes to zero then back to one.

You would call it in a timed function that is triggered by the sensor flag.

Try to keep loop() clear, just timer and Blynk run.

In fact you can keep Blynk.begin() in setup and use the connect and disconnect functions available with Blynk.

If it can be done in an IF statement, then I would do that and keep the loop() to only the essentials, typically; and;.

I was able to fix the issue by moving the from outside the IF statement. Thank you all for your help.