SimpleTimer and Delay() question

I’m using SimpleTimer in my Blynk code on a Wemos D1 Mini, all works great and learning how to use Simpletimer. Question If I need a short time delay to read sensor inside my void myTimerEvent() can I use Delay()? What would be best function to use? Thanks!

Because delay() is blocking (i.e. absolutely nothing else gets done on the MCU until the delay is finished sitting around on it’s backside), It is to be avoided wherever possible for wireless connected projects… else it tends to cause disconnections.

Timers are the answer… yes, it is a bit of extra coding and logic, but it is non-blocking and much more elegant in operation.

BAD :rage:

BLYNK_WRITE(Vx)  // Virtual button on Vx to activate action
{
  int BTN = param.asInt();
    if (BTN == 1) {
      digitalWrite(pin, HIGH);  // Set pin high
      delay(5000); // NOOOOOooooooooo
      digitalWrite(pin, LOW);  // Set pin Low
      }
}

GOOD :heart_eyes:

BLYNK_WRITE(Vx)  // Virtual button on Vx to activate action
{
  int BTN = param.asInt();
  if (BTN == 1) {
    ActionON;  // Run ActionON function
  }
}

void ActionON()
{
  digitalWrite(pin, HIGH);  // Set pin high
  timer.setTimeout(5000L, ActionOFF);  // Run ActionOFF function in 5 seconds
}

void ActionOFF()
{
  digitalWrite(pin, LOW);  // Set pin Low
}

And this is just a dirt simple code solution that is within my coding experience level… there are probably even more elegant ways of coding a non-blocking delay.

EDITED - switched code to setTimeout from setTimer

8 Likes

One little thing @Gunner, use timer.setTimeout() otherwise it will keep running and consume. The ActionOn() can also just be put in Blynk write. Each declaration of a void costs memory :wink:

Maybe that’s where mine is going… to many voids in my life :stuck_out_tongue:

I am still learning all the timer functions myself, and keep forgetting how to use some of them.

And I should have thought about the ActionON/BLYNK_WRITE() merger… doh :blush:

Thanks!

Ah, Google my friend :stuck_out_tongue:

I think this will work?.. too hot out to think, let alone sit at the computer compiling code…

GOODer :blush:

BLYNK_WRITE(Vx)  // Virtual button on Vx to activate action
{
  int BTN = param.asInt();
     if (BTN == 1) {
     digitalWrite(pin, HIGH);  // Set pin high
     timer.setTimeout(5000L, ActionOFF);  // Run ActionOFF function in 5 seconds
     }
}

void ActionOFF()
{
  digitalWrite(pin, LOW);  // Set pin Low
}
3 Likes

Then there is this one again that will make @Jamin happy :stuck_out_tongue:

8 Likes

Nothing makes me happier :rainbow: :rainbow: :rainbow: :rainbow: :rainbow: :rainbow:

2 Likes

Great coding guys! I’ve learned something new too now :smiley:

Sadly this does not work, it would have been sweeter

BLYNK_WRITE(Vx) 
{
    param.asInt() ?  timer.setTimeout(5000L, [](){ digitalWrite(pin, LOW) } ) : ;
}

The “eval ? true : false;” shorthand only takes void functions it seems :stuck_out_tongue: at least from my short testing. And needs both the true and false statements.

I guess this is cool enough

BLYNK_WRITE(Vx) 
{
   if ( param.asInt()) { digitalWrite(pin, HIGH); timer.setTimeout(5000L, [](){ digitalWrite(pin, LOW) } ) } ;
}
1 Like

Careful, the “digitalWrite(pin, HIGH)” is missing now :blush:

My bad, not easy from the phone :stuck_out_tongue: fixed

Just found this post and tried searching to find what this “[ ] ( )” meant…

Does anyone have a link that could help?

Check this post from @Gunner

C++ Blynk - Code Examples for Basic Tasks (Work in Progress)

Thanks.

A post was split to a new topic: How can I pass parameters to this lamba timer expression