Why can’t we use timer function such as .timer.setTimeout(5000L, { digitalWrite(pin, LOW) } ); inside the loop.
Why would you want to do it? It’s a bit like trying to load a barrow just by repeatedly sticking the showell into the ground. Perhaps you don’t understand the idea lying behind timers??
I want to use timer function instead of delay as it is causing disconnection from server due to delay function inside loop. So what must be the alternative instead of placing the timer inside a void . This method is causing too much void function
You are not allowed to use timers the same way as you are using delay()!!
The timers are what the name suggest: A timingly recalled routine (part of code)
Please look at the examples and at the timer manual :
http://docs.blynk.cc/#blynk-firmware-blynktimer
Just note,that inside the loop you will find only the timer.run()
- It is by no means comparable to delay: Its purpose is to manage timer tasks.
My suggestion:
- create a flag variable say timerDone.
- Initialise it to 0.
- Instead of the delay In the loop() routine insert the code:
delayTimer();
if (timerDone) DoSomething; - In code before loop, declare the routine delayTimer.
void delayTimer()
{
digitalWrite(pin, LOW);
timerDone =1;
}
Edit: The loop will like:
void loop()
{
Blynk.run();
timer.run();
if (Condition_met) delayTimer();
if (timerDone) DoSomething();
}
Since I was responding to some one’s problem, I thought about the issue and made/wrote this up. If senior members in this group think this is elegant way of replacing delay statements with timers and avoid heartbeat miss etc., maybe this can be published in the knowledge base as a recommended alternative to using delays.
Well this old fart (but young at heart ) is still wondering why this OP needs a delay in the loop?? If you only want something to run for say once every 10 seconds, then put it in a timed function that runs once every 10 seconds… no need for a delay, fake or otherwise, in the loop.
I don’t know @mohan_sundaram. If someone REALLY needs to hold the program flow, but not causing the Blynk to timeout, I’d try the following:
#define DELAY5000 5000L
void loop(){
//some code
for (unsigned long start = millis(); millis() - start < DELAY5000; ) { //start delay, but do not block Blynk "stuff"
Blynk.run();
}
//some code
BLynk.run();
}
This will definitely hold the loop() without interrupting the Blynk… I’m not using it anyway