Pages: [1]   Go Down
Author Topic: MsTimer2 - Calling set() more than once  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
}
Logged

Pages: [1]   Go Up
Jump to: