Simple timer library

Dear Blynkers,

I have a small issue regarding the simple timer library . After going through some docs and examples i do have an idea about it but not a complex one either . As in there are functions such as delete timer , so on and whther we can use while loops inside of a timer.settime out . Please be kind enough to paste a link or to explain me what each function does accordingly means alot

Thank you,
Aaron fernando

https://playground.arduino.cc/Code/SimpleTimer/

i did read it @Gunner but the point is i dont understand the reason for deleting timers and enabling the timers etc.

Thank you,
Aaron fernando

It’s a fairly simple concept, but maybe you’d be better-off talking about your specific “small issue” than having the whole library explained in excruciating detail.

I assume that it’s actually the BynkTimer library that you’re using instead of SimpelTimer?

Pete.

What is the reason for any variations in libraries and code… to get things done in various ways as the individual needs and circumstances require.

You want a timer running all the time, you set one. Then for some reason you want to stop for awhile, you do so. Then later you need to have it restarted… well there is a command for that as well :stuck_out_tongue_winking_eye:

As for the rest… you best bet is to search for and read existing examples… I have a few in here…

And as @PeteKnight pointed out, BlynkTimer, while based on SimpleTimer, does have some small changes in how you set it up.

No need to load the SimpleTimer library.

Use BlynkTimer timer; in your pre-setup.

Other then that, they are effectively the same for documentation purposes. The most notable effect of Blynk’s version is the upgrade to 16 iterations per timer.

Dear @Gunner and @PeteKnight below is the code which im trying to blink a LED my goal is ,
to increase the led brightness for 1 second and keep that brightness for 2 seconds and again decrese the led brightness with in a 1 second frame , below is the code . It does not work properly i know there were examples which mr.gunner have posted but i want to know whats the issue with the below code is.

BLYNK_WRITE(V5) {
  
  timer.setTimer(4000L, []() {

    timer.setTimer(20L, []() {
      analogWrite(Red, Fade_Up);
      Fade_Up = Fade_Up + 5 ;
    }, 50);
    
    timer.setTimeout(2000L, []() {
      analogWrite(Red, 255);
    });
    
    timer.setTimer(20L, []() {
      analogWrite(Red, Fade_Down);
      Fade_Down = Fade_Down - 5 ;
    }, 50 );
    
    Fade_Up = 0 ;
    Fade_Down = 255 ;
    
  }, 3); 


}  

Please be kind enough to explain whats the issue.

FYI i have already declared fade up and down as int global variables
.

Something to consider is that setting those three nested Lambda setTimer functions doesn’t
mean that the code runs them in consecutive order… rather, it initialises them all at the same time inside the primary Lambda cycle.

So basically you are fading up, setting to full and fading down all at the same time.

You need to use flags and only call function #2 once function #1 finishes cycling, and so on.

Very tricky to visualise… try flowcharting the code then rebuilding one section at a time.

i have been trying many methods to solve this issue , but i am not still able to find a proper solution here are the codes i tried after @Gunner advised ,


  timer.setTimer(4000L, []() {

    flag_fading_up  = 1 ;
    flag_holding  = 0 ;
    flag_fading_down  = 0 ;

    if (flag_fading_up == 1) {

      timer.setTimer(20L, []() {
        analogWrite(Red, Fade_Up);
        Fade_Up = Fade_Up + 5 ;
      }, 50);

      flag_fading_up  = 0 ;
      flag_holding  = 1 ;
      flag_fading_down  = 0 ;

    }

    if (flag_holding == 1) {

      timer.setTimeout(2000L, []() {
        analogWrite(Red, 255);
      });

      flag_fading_up  = 0 ;
      flag_holding  = 0 ;
      flag_fading_down  = 1 ;

    }
    if (flag_fading_down == 1) {
      timer.setTimer(20L, []() {
        analogWrite(Red, Fade_Down);
        Fade_Down = Fade_Down - 5 ;
      }, 50 );

      flag_fading_up  = 1 ;
      flag_holding  = 0 ;
      flag_fading_down  = 0 ;
    }

    Fade_Up = 0 ;
    Fade_Down = 255 ;

  }, 3);


  }

and this is the other code which i tried both didnt work,

  flag_fading_up  = 1 ;
  flag_holding  = 0 ;
  flag_fading_down  = 0 ;

  if (flag_fading_up == 1) {

    timer.setTimer(20L, []() {
      analogWrite(Red, Fade_Up);
      Fade_Up = Fade_Up + 5 ;
    }, 50);

    flag_fading_up  = 0 ;
    flag_holding  = 1 ;
    flag_fading_down  = 0 ;

  }

  if (flag_holding == 1) {

    timer.setTimeout(2000L, []() {
      analogWrite(Red, 255);
    });

    flag_fading_up  = 0 ;
    flag_holding  = 0 ;
    flag_fading_down  = 1 ;

  }

  if (flag_fading_down == 1) {
    
    timer.setTimer(20L, []() {
      analogWrite(Red, Fade_Down);
      Fade_Down = Fade_Down - 5 ;
    }, 50 );

    flag_fading_up  = 1 ;
    flag_holding  = 0 ;
    flag_fading_down  = 0 ;

    Fade_Up = 0 ;
    Fade_Down = 255 ;
  }


}

like mr @Gunner adviced earlier what happens is the led doesnt change some times it changes but nothing happens the led just turns on and keeps it like that.