General Question - Program flow / Interrupt?

I want to write code and once something in Blynk happens my program should be updated or interrupted.

But there’s the blynk run function which seems need to be run in loop to detect Blynk changes.

Is there a way to run my personal code and stop it and execute a function or update my code or once something in Blynk is set or done?

Just check the examples and documentation about Virtual Pins - everything is there.

I read that but didn’t understand how I can use this exactly.

I did my last program more than 20 years ago

Thats why I need further information.

To be clear I understand Virtual Pins but lets say I got a loop doing some fancy LED lighting. I then would like my arduino to stop the routine and do sone other once such virtual pin gets updated.

How would I do this?

By using if/else statements probably. That shouldn’t be much different from 20 years ago.

If (ledstatus == 1)

If you combine this in a BLYNK_WRITE() function the possibilities are endless :slight_smile:

So I don’t need the BLYNK-RUN() at all when using BLYNK_WRITE() ?


Let’s say i got a loop doing something for one minute. Is there a way to call a interrupt function when a virtual pin is set or is the way to include such if statement in this loop?

No, you always need That is where the Blynk magic happens.

An interupt is a hardware thing. I think Pin2 on the Arduino can handle interupts, but in most cases it is enough to have a timer. The ideal loop() looks like this:

void loop()
{;; // initiates timer object

If you have a timer which check every 250ms for example, that should be fast enough in most cases.

Could I for example attach an interrupt to a virtual pin?

No, that is not possible as far as I know because a virtual pin is software and and interrupt is a hardware thing. You have to stop the cpu from doing what it’s doing and that can’t be accomplished with virtual pins.

What is it you want to do anyway? We may be able to give you some hints to get it working like you want :slight_smile:

1 Like

I got multiple projects.

One is the best example.

This will be a LED Light that will have multiple lighting modes that shall run constantly.

While it runs it shall stop and act on buttons of the app.

For example I write a lighing function that is called from the main loop and runs for one minute before the next one is called.

This would mean that I constantly in every lighting loop need to check the virtual pins, this seems very complicated…

Or is the inly way to solve that the timer library?
But even then I would need to put it in each lighting loop right?

You cannot directly interrupt a virtual pin.
Instead of interrupts you can use polling. Suppose that your system has three lighting modes, a 250 ms blinking, 500 ms blinking and 1 sec blinking and you want each mode to be running until you stop it and try out another one.
Define three different functions each with the functionality mentioned above and in each function check for status of the virtual pins associated with that lighting mode on the Blynk application(USE A SWITCH IN BLYNK APPLICATION AND NOT A PUSH).
Now, the question is how and when will you check this.

void blink250()
int num=1;
num=param.asInt() //according to me it will be 0 for a LOW value of the Switch

What you do here is, after every blink you check the status of the pin associated with that mode on the blynk app. If it is low you go back to the loop.
In the loop continuously check for the status of all the three virtual pins in the same manner and call the three functions accordingly.
I am not sure if I have understood your problem correctly and whether the solution i gave you will work for you but i definitely did try my best.

@vshymanskyy please correct me for this if it is wrong

num=param.asInt() //according to me it will be 0 for a LOW value of the Switch



Sice I plan to use about 10 lighting modes I need to change them with button press. So Switch is no option I can use.

See, you have to understand that what you are using for your project is a micro controller based board and not a microprocessor based board like Intel Gallileo. You cannot multitask with it.
What you want can only be possible if we use 10 external interrupts which is out of question because no micro controller provides 10 external interrupt pins.
In fact, this whole idea cannot work with the concept of interrupts.
If at all you want to use 10 Push Buttons I suggest use two Arduino Boards, one to continuously poll your input on the Blynk app and the other to carry out the functionalities you want.

Then workout some logic to control the latter with the first board.

That would be a way.

But you don’t need 10 Interrupt fir that. One could do that with virtual pins and ONE interrupt that signals any button press.

But I don’t know if such is possible at all with Blynk

You can definitely use a push button for this but you have to count the pushes and act accordingly:

  int cycleNr = 0;
  if((param.asInt == 0) && (cycleNr != 0))
    cycleNr = 0;

Based on var cycleNr (you probably need that as a global var) you can change modes in another sub routine which is called for example every 250ms. That should be short enough to do it without notable delay.

So if I’m right I could use Timer Interrupt for checking the Button right?

Does the simple timer library the same job as timer interrupt or won’t it work here?

The SimpleTimer is not an interrupt, it just runs things every x-period. Interrupts, as the name says, interrupts the CPU clock cycles. I still don’t see the need for using a real interrupt using blynk since it’s so blazingly fast using it’s own code.

I think my solution should work fine without using interrupts. They are difficult to handle anyway (in a good way that is).

Can use your code but one of my problems was I got functions that might run for multiple seconds.

Therefore I either use an interrupt or need to include one Blynk read in every of the longer subroutine.

btw. has the to be called also every lets say 250ms or is it ok to just call it after a long time e.g. after minutes in the main() again?

Until now I don’t really understand the purpose of (besides “the magic happens” which isn’t that helpful :wink: )

@Gorkde there is a heartbeat timeout set at between 5 and 10 seconds that Blynk looks for from your device. Your device may try to reconnect if it misses the heartbeat but then again it might not. A second or two is probably ok but I wouldn’t want to go much beyond this.

So this should work if I call blynk. Run() every 250ms in my subroutine or does it need tu run continously?