MsTimer2

Hi,

I'm trying to use the mstimer2-function:

#include <MsTimer2.h>

// Switch on LED on pin 13 each second

void flash() {
static boolean output = HIGH;

digitalWrite(4, output);
output = !output;
}

void setup() {
pinMode(4, OUTPUT);

MsTimer2::set(500, flash); // 500ms period
MsTimer2::start();
}

void loop() {
}

The Program doesn't work on my arduino Mega 2560. The LED doesn't flash. But the "blink"-example works perfectly. I've downloaded the mstimer2.h-libary and copied it into the arduino libary-folder.

Does anybody know what I'm doing wrong ?

And what do you think how many times a analog-sensor can be read out per second using the mstimer-function ? And what happens if a command takes longer than the time specified in MsTimer::set(periodtime,....) ?

Thanks,
Maik

Maik_:
Does anybody know what I'm doing wrong ?

you have to restart the timer...

void flash() {
  static boolean output = HIGH;
  digitalWrite(4, output);
  output = !output;
  MsTimer2::start(); //<<<<<<<<<<
}

This link explains how long an analogRead takes.

The Program doesn't work on my arduino Mega 2560. The LED doesn't flash. But the "blink"-example works perfectly. I've downloaded the mstimer2.h-libary and copied it into the arduino libary-folder.
Does anybody know what I'm doing wrong ?

What is connected to pin4? The onboard led called in the "blink" is attached to pin 13.

And what do you think how many times a analog-sensor can be read out per second using the mstimer-function ?And what happens if a command takes longer than the time specified in MsTimer::set(periodtime,....) ?

The standard analogRead() take about 110 microseconds.

The function executed at the time specified is an interrupt service routine (ISR). If execution of the ISR takes longer than the time to the next arriving interrupt, the new interrupt will be noted, and placed in a queue for execution when the running ISR is completed.

ISR's should be short and quick to run. The fact that you are asking the question indicates that you may have the wrong idea of what you want to do on a 500 ms timer interrupt.

@BulldogLowell

you have to restart the timer...

The library example does not show restart()

/*
  MsTimer2 is a small and very easy to use library to interface Timer2 with
  humans. It's called MsTimer2 because it "hardcodes" a resolution of 1
  millisecond on timer2
  For Details see: http://www.arduino.cc/playground/Main/MsTimer2
*/
#include <MsTimer2.h>

// Switch on LED on and off each half second

#if ARDUINO >= 100
const int led_pin = LED_BUILTIN;	// 1.0 built in LED pin var
#else
const int led_pin = 13;			// default to pin 13
#endif


void flash()
{
  static boolean output = HIGH;
  
  digitalWrite(led_pin, output);
  output = !output;
}

void setup()
{
  pinMode(led_pin, OUTPUT);

  MsTimer2::set(500, flash); // 500ms period
  MsTimer2::start();
}

void loop()
{
}

Thanks... I've added "MsTimer2::start();", but nothing happens. The LED doesn't flash.
On Pin 4 an LED is connected.

digitalWrite(4, HIGH) in the loop-function works and the LED flashes. So the LED is connected properly. But it doesn't solve the MsTimer-problem.

I'd like to read out an acceleration-sensor every few milliseconds and write the data to a sd-card...

#include <MsTimer2.h>

// Switch on LED on pin 13 each second

void flash() {
static boolean output = HIGH;

digitalWrite(4, output);
output = !output;
MsTimer2::start(); // <<<<<<<<
}

void setup() {
pinMode(4, OUTPUT);

MsTimer2::set(500, flash); // 500ms period
MsTimer2::start();
}

void loop() {
}

Thanks!

Maik_:
Thanks!

I googled and saw that there is a patch needed for Mega 2560. Is that older than the latest update of the MSTimer2 library?

Yes you are right.
I've fixed the problem by downloading the latest version of the msTimer2 libary. Now it works and the LED flashes... Thanks.