How many times I can use Blynk Timer?

Hi, please how many times I can use Blynk Timer and if limited is there an alternative I can use like up to 30 times?

in setup like this
timer.setInterval(1000L, function1);
timer.setInterval(1000L, function2);
…30x

thanks!
L.

Basically each timer object can run 16 timers.

Example:

BlynkTimer timer1 16 instances
BlynkTimer timer2 16 instances
BlynkTimer timer3 16 instances
BlynkTimer timer4 16 instances

As far as i have understood there is no limitation. But you will have to stagger the timers. There should be no delays inside the functions called by the timers.

Go through this topic. It just has all the important stuff.

1 Like

Thanks Madhukesh, there is still one thing I lack to understand as per your example:

timer.setInterval(1000L, myFunction1);
timer.setInterval(1020L, myFunction2);
timer.setInterval(1040L, myFunction3);
timer.setInterval(1080L, myFunction4);
timer.setInterval(1100L, myFunction5);
timer.setInterval(1120L, myFunction6);
timer.setInterval(1140L, myFunction7);
timer.setInterval(1160L, myFunction8);

this will work but only 16 timers like this correct? How do I do those other 16 and more?

shall I create those object in the initialization where you place the “BlynkTimer timer;” ?

BlynkTimer timer
BlynkTimer timer2

this is not a smooth way how to do things using like 30 timers but I need to run multiple functions in parallel for a specific time and the usual millis() where is while is blocking everything :frowning:

Yes. You are right.

timer.setInterval(1160L, myFunction16);

16th instance

timer2.setInterval(1160L, myFunction17);
And so on.

If you post your sketch or explain what you are trying to achieve someone can show you a easy way !!

I can’t imagine a situation where 30+ timers are necessary. My most complex sketch only uses 5 timers!

Maybe if you explained what you are trying to achieve, and why you think you need so many timers then we’d be able to suggest a better code structure.

Pete.

My most complex one has 16 timers :stuck_out_tongue_closed_eyes:

Thanks Pete again!

So the whole problem is to be able to call multiple functions, run them for a predefined time period at the same time. It is for the whole house where I have window Blinds. Sometimes I want to start rolling a window in room 1 and while it is rolling start rolling another window.

The only thing I was able to come up with which does not use blocking loops was that all these functions are being called all the time using timers and the trigger to run the code inside would be a variable state with if condition. The variable state I change with the virtual buttons in my phone.

One motor needs 2 functions, one for going up another for going down with some conditions. I have 5 motors and that is only one type of procedure like go down/up all the way. I have 4 types of these functions go half way down or open the blinds only - the angle and so on.

The whole code is done it has 2000 rows and it uses Millis functions with while which does this annoying thing once you start rolling one blind untill the loop finishes you cannot do anything else

I forgot how to post a code here properly

(```)

Copy the provided triple backticks inside the brackets and paste them at the start and the end of your code.

That sounds like a situation where a timeout timer, probably as part of a lambda function, is a far better solution than having an interval timer that is running all the time.

That’s bad coding. If you did it properly then you wouldn’t need to wait. However, there is no need to use millis comparison in most situations, timeout timers are a much better option in most situations.

You should read this:

Pete.

Thanks Pete will try the timers again I have tried already and it did smth weird will try one more time

Pete I’m trying to use the timers but it wont work - it ignores the T1 completely - the led is not turning on, the T2 works but it never stops - what do you think is wrong here

the goal is to light the pin_led or pin_led2 for 2 seconds





#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
BlynkTimer timer;


#define pin_led 21
#define pin_led2 13

int state = 0;
int state2 = 0;
int Virtual_Button_Led1;
int Virtual_Button_Led2;

BLYNK_WRITE(V1)
{
  Virtual_Button_Led1 = param.asInt();
  if (Virtual_Button_Led1 == 1)
  {
    timer.enable(T1);
  }
}

BLYNK_WRITE(V2)
{
  Virtual_Button_Led2 = param.asInt();
  if (Virtual_Button_Led2 == 1)
  {
    timer.enable(T2);
  }
}


void LEDD()
{
  digitalWrite(pin_led, 1);
}

void LEDD2()
{
  digitalWrite(pin_led2, 1);
}


void OFF()
{
  if (T1 == false)
  {
    digitalWrite(pin_led, 0);
  }
  else if (T2 == false)
  {
    digitalWrite(pin_led2, 0);
  }
}




void setup()
{
  Serial.begin(115200);
  delay(100);
  pinMode(pin_led, OUTPUT);
  pinMode(pin_led2, OUTPUT);

int T0 = timer.setInterval(500L,  OFF);
int T1 = timer.setTimer(200L,  LEDD, 10);
int T2 = timer.setTimer(200L,  LEDD2, 10);

timer.disable(T1); //turn the timers off when the program starts
timer.disable(T2);
BlynkEdgent.begin();
}

void loop() 
{
  BlynkEdgent.run();
  timer.run();
}

So, I’ve suggested that you use timeout timers, preferably in a lambda function, and you’ve gone down a totally different route, then you’re telling me it doesn’t work.

Use timeout timers!

Pete.

Pete I don’t know how to do it I’m trying to make sense out of your instructions, could you please show example?

how would you modify my code to call just the LEDD function 2 seconds and turn the pin_led off?

I hope I’m not asking too much sorry for that

how do I specify which timer here?

  timer.setTimeout(5000L, []() 
  {  
    // When the timer completes, any code here will be executed
  }); 

You’re missing the point of timeout timers. They aren’t used to call a timer that is defined in void setup.
This example uses the BlynkTimer timer object, which needs to be defines and an object…

BlynkTimer timer;

and ‘fed’ in your void loop…

void loop()
{
  timer.run(); // call the BlynkTimer object
  Blynk.run(); // perform a handshake with the Blynk server
}

other than that, you simply use the timeout timer to wait x milliseconds then execute the code ‘completed’ code.

This is why you don’t need to define 30+ interval timers if you simply want to trigger an action, wait a period of time, then perform another action.

Pete.

Pete I did it: it works!

BLYNK_WRITE(V1)
{
  Virtual_Button_Led1 = param.asInt();
  if (Virtual_Button_Led1 == 1)
  {
   digitalWrite(pin_led, 1);
   timer.setTimeout(time_to_wait, []() 
  {  
   digitalWrite(pin_led, 0);
  }); 
  }
}

BLYNK_WRITE(V2)
{
  Virtual_Button_Led2 = param.asInt();
  if (Virtual_Button_Led2 == 1)
  {
   digitalWrite(pin_led2, 1);
   timer.setTimeout(time_to_wait, []() 
  {  
   digitalWrite(pin_led2, 0);
  }); 
  }
}

Thanks again Pete for the patience I’m learning

Are you using the Virtual_Button_Led1 elsewhere?

If not then this could be simplified to…

BLYNK_WRITE(V1)
{
  if (param.asInt)
  {

Pete.