I propose FlexiTimer 2 to be added to http://arduino.cc/en/Reference/Libraries, in the “Timing” section.
What is it?[/b]
An Arduino library to use timer 2 with a configurable resolution. Based on MsTimer2 by Javier Valencia.
Where is the code?[/b]
Why is this useful?[/b]
Both Metro and MsTimer2 (which are already listed in the “Timing” section of http://arduino.cc/en/Reference/Libraries) limit the end user to a millisecond resolution. Suppose you need an interrupt service routine to fire 2000 times per second. You just can’t with those libraries. You’re forced to write code at a lower level, or write a library that makes it easy, like I did here.
My use case was the need to drive an 8x8 RGB led matrix that uses multiplexing, which therefore requires 8 leds (1 row active at a time) x 3 colors = 24 pins to manipulate in PWM mode. I’m doing this using an Arduino Mega, which only has 14 PWM pins. Hence I needed to simulate PWM in software, thus I had to use interrupts. I needed 8 rows x 6 intervals x 60 refreshes per second = 2880 interrupts per second.
With FlexiTimer2, that’s easy:
**__</strong> <strong><strong>**FlexiTimer2::set(1, 1.0/2880, callback); FlexiTimer2::start();**</strong></strong> <strong>__**
Which is just one extra parameter compared to MsTimer2:
**__</strong> <strong><strong>**MsTimer2::set(1, callback); MsTimer2::start();**</strong></strong> <strong>__**
In fact, this even works and defaults to MsTimer’s behavior (i.e. the default value of the second parameter is 0.001, i.e. a resolution of 1000 interrupts per second, or 1ms):
**__</strong> <strong><strong>**FlexiTimer2::set(1, callback); FlexiTimer2::start();**</strong></strong> <strong>__**
For lack of inspiration, I called it FlexiTimer2. IMVHO, it could easily replace MsTimer2 if FlexiTimer2’s documentation is improved somewhat.
It was written for the project associated with the “Mobile & Pervasive Computing” course at Hasselt University in Belgium.
I gladly welcome your feedback