Using delay() to stagger timer functions - does it work?

It has been suggested to me that this code would stagger the functions being called by 100ms.

void setup()
{
timer.setInterval(500L, function_1);
delay(100);
timer.setInterval(500L, function_2); 
}


void loop()
{
timer.run
}

I wonder if it would just cause a 100ms delay in the void setup but have no impact on staggering the timers.

Could anyone clarify?
Thanks

Code in setup() functions like any other but once you hit the closing brace control passes to loop() thereafter.

What's the purpose of 'staggering'?

dougp:
Code in setup() functions like any other but once you hit the closing brace control passes to loop() thereafter.

To be clear, it won't stagger the timers once timer.run is being called from the loop ?

dougp:
What's the purpose of 'staggering'?

I have two functions in the code, it has been suggested that they could be taking too long to process when called at the same time. I can post the full code but wanted to clarify this single point.

It really depends on which timer library you are using. There are many.

Why don't you find out? Add this to your code and watch the serial monitor

void function_1() {
  Serial.print("function1: ");
  Serial.println( millis() );
}

void function_2() {
  Serial.print("function2: ");
  Serial.println( millis() );
}

blh64:
It really depends on which timer library you are using. There are many.

Yes, please tell us which timer library you're using. Of course, if you had posted complete code we would know by now.

Thanks both!

@blh64 I will try that code now and see what happens. I am using the Blynk timer. The library is <BlynkSimpleEsp8266.h>.

@gfvalvo I will post the code next, I understand what you mean. I didn’t want to confuse the situation as the problem I am having is unusual (using Blynk terminal widget, I am getting duplicate code in the terminal window).

I have of course posted on Blynk forums, but I wondered about this specific thing (staggering timers).

Many thanks

Here is my test code as suggested by @blh64

#define BLYNK_PRINT Serial // Defines the object that is used for printing Blynk stuff
#include <BlynkSimpleEsp8266.h>

  
//Blynk credentials

char auth[] = ""; // v009 TEST Breadboard auth token
//Wifi credentials
char ssid[] = "";  //Enter your WIFI Name
char pass[] = "";  //Enter your WIFI Password
//Server credentials
char server[] = ""; 
int port = xxxx;   



//SimpleTimer timer; //setup simple timer to call functions on a timed basis from void loop
BlynkTimer timer; // v009 improvement http://docs.blynk.cc/#blynk-firmware-blynktimer 



void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, server, port);  //connects to Wifi and LOCAL Blynk server (running on raspberry pi)
  
  //Setup Blynktimers
  timer.setInterval(1000L, function_1); 
  delay(500); 
  timer.setInterval(1000L, function_2);  


void loop()
{
  Blynk.run();  //This function should be called frequently to process incoming commands and perform housekeeping of Blynk connection.
  timer.run();  //Initiates SimpleTimer to runs timed functions
 }


void function_1() 
{
  Serial.print("function1: ");
  Serial.println( millis() );
}

void function_2() 
{
  Serial.print("function2: ");
  Serial.println( millis() );
}

Here is the output:

20:11:21.736 -> [5021] Connecting to xxx
20:11:22.245 -> [5523] Connected to WiFi
20:11:22.245 -> [5523] IP: 192.168.1.xx
20:11:22.279 -> [5523] 
20:11:22.279 ->     ___  __          __
20:11:22.314 ->    / _ )/ /_ _____  / /__
20:11:22.348 ->   / _  / / // / _ \/  '_/
20:11:22.382 ->  /____/_/\_, /_//_/_/\_\
20:11:22.416 ->         /___/ v0.6.1 on ESP8266
20:11:22.450 -> 
20:11:22.450 -> [5599] Connecting to 192.168.1.xx
20:11:22.485 -> [5656] Ready (ping: 8ms).
20:11:23.435 -> function1: 6724
20:11:23.911 -> function2: 7224
20:11:24.418 -> function1: 7724
20:11:24.930 -> function2: 8224
20:11:25.435 -> function1: 8724
20:11:25.938 -> function2: 9224
20:11:26.411 -> function1: 9724
20:11:26.919 -> function2: 10224
20:11:27.424 -> function1: 10724
20:11:27.935 -> function2: 11224
20:11:28.442 -> function1: 11724
20:11:28.912 -> function2: 12224
20:11:29.417 -> function1: 12724
20:11:29.926 -> function2: 13224
20:11:30.434 -> function1: 13724
20:11:30.915 -> function2: 14224
20:11:31.425 -> function1: 14724
20:11:31.933 -> function2: 15224
20:11:32.441 -> function1: 15724
20:11:32.918 -> function2: 16224

So it appears that the delay() does in fact cause the timers to be staggered.

I’m assuming when the timers are first inititated in void setup() the millis is recorded then?

877:
I'm assuming when the timers are first inititated in void setup() the millis is recorded then?

Basically. Take a look at the source code in 'BlynkTimer.cpp'. You'll see that the 'setInterval()' method calls 'setupTimer()' which records the current value of millis() in the 'prev_millis' member of its internal timer structure. All timing going forward is based on that initial reading.

IMO, you should always try to dig into the source code of libraries you're using when you really want to know the guts of how they work. I've learned a great deal that way.

gfvalvo:
Basically. Take a look at the source code in 'BlynkTimer.cpp'. You'll see that the 'setInterval()' method calls 'setupTimer()' which records the current value of millis() in the 'prev_millis' member of its internal timer structure. All timing going forward is based on that initial reading.

IMO, you should always try to dig into the source code of libraries you're using when you really want to know the guts of how they work. I've learned a great deal that way.

Thanks, I understand now.

It never occurred to look in the library files, thanks for the advice!! :slight_smile: