Go Down

Topic: MsTimer2 - Calling set() more than once (Read 696 times) previous topic - next topic

wilkosoft

I'm using MsTimer2 to process messages at specific intervals, so I need to call MsTimer2::set() repeatedly so I can set different times. Perhaps this is the wrong library to use for this sort of thing but it's so convenient....

Anyway I found that my process just died when set was called a second time, until I tried calling set with 0 time and 0 funtion  pointer, and it all started working fine!

Here's a snippet:

#include <MsTimer2.h>

unsigned long time;
volatile boolean timerSet=false;

void setup()
{
 time = 1;
 pinMode(13,OUTPUT);
 digitalWrite(13,HIGH);
 delay(1000);
 digitalWrite(13,LOW);
 delay(1000);
 timerSet=false;
}

void loop()
{
 if(!timerSet)
 {
   MsTimer2::set(0, 0);
   time+=10;//increase our interval by 10 ms every time (arbitrary hack for demonstration purposes)
   MsTimer2::set(time, DoAction);
   MsTimer2::start();
   timerSet=true;
 }
}

void DoAction()
{
 static boolean output = HIGH;
 digitalWrite(13, output);
 output = !output;
 MsTimer2::stop();//we only want the timer to call once
 timerSet=false;
}

Go Up