[SOLVED] About timers and interruptions

Hello everyone,

I'm running a project in wich I acquire data from pressure sensors and process it.
I want to establish a fix sampling frequency, so I wanted to set up a timer in order to get samples every 10mS. I know I can easily do that with millis(), but I searched on internet and found two interesting Timer libraries that are suitable for me. However, they have an important difference and I need your help to know which one is better.

I remember from university that using interruptions is considered a good practice, but I have 2 concerns.
First, the library uses millis(), that also uses interruptions, and won't update while I'm in the handler. I guess I can solve this using a handler with no more than three instructions, but I don't know if it's enough.
Second, I'm trying to keep the main sketch as tidier as possible and, as I can't put the handler inside a class, I should leave it there in the main. Is this considered a "bad practice"? How bad it is?

That's it. Which one do you recommend? Why?

Thank you very much!

SimpleTimer Library (Arduino Playground - HomePage) has a method that calls a regular function every X mS. Really simple.

This library is doing exactly what you described in the beginning: it checks millis() and reacts on it.

Timer Library (Arduino Playground - HomePage) has a method that changes the state of a pin every X mS. Then you need to attach an interrupt to that pin, and go on from that.

Same is true for this library but in this case you want to do an external loop, so you activate a pin and let it trigger an interrupt. That's just a big waste of resources.

I remember from university that using interruptions is considered a good practice

This is not true for most cases. If you can avoid the use of interrupts, avoid it. Use them if you have no other option to reach your specs.

Second, I'm trying to keep the main sketch as tidier as possible and, as I can't put the handler inside a class, I should leave it there in the main. Is this considered a "bad practice"? How bad it is?

You can put the handler inside a class you just have to call it from a global code part (where the interrupt vector points to). An interrupt handler cannot be inside main(). And in the Arduino world you usually don't have a main() routine at all as the IDE is hiding that from you.

as tidier as possible

I'll bet they didn't teach you that at university.

Interrupts are useful in handling events that occur at undefined times, pushbuttons, alien invasions, etc., or for scheduled events that sor run in the background, like the overflow counter used by millis(). If the main function of the processor is the timed activity, it's often tidier to deal with it directly, even without the convenience of a library. Task schedulers are quite simple to implement and by doing so yourself you learn more and ultimately have greater control.

A great place to start is here.

Another question you can answer now is how critical is your timing? When you say x milliseconds, are there serious downside repercussions if it were x +/- 1 or 2 milliseconds? If we're talking about a robotic hand grasping an egg, maybe serious.

If you're still bent on using somebody else's code, instead of experiencing the joy of developing your own, I have used TimerOne in the past and can't complain.

@OP

DKWatson:
Another question you can answer now is how critical is your timing? When you say x milliseconds, are there serious downside repercussions if it were x +/- 1 or 2 milliseconds? If we're talking about a robotic hand grasping an egg, maybe serious.

If you're still bent on using somebody else's code, instead of experiencing the joy of developing your own, I have used TimerOne in the past and can't complain.

Based on the above opinion, you can directly program TC1 to offer you an exact interval of 10 ms; the strategy could be polled interrupt or vectored interrupt. However, this approach could be a little bit tiring as the programming involves register level instructions.

Pseudo Codes:

set TC1 operating mode in normal counting mode
determine clocking frequency for TC1
Keep the TC1 OFF
Compute pre-set count for the overflow to occur at 10 ms
Load the pre-set value in TCNT1 register
Start TC1

Thanks to everyone. I finally solved it, and post what I learnt hoping it will be useful for someone with the same problem:

  • When you use a Callback function (for example, in attachInterruppt), and the handler belongs to a class, it needs to be declared as STATIC in order to prevent the overload of this->.

  • STATIC function can only use global variables if they are also declared as STATIC.

  • if you declare a variable as STATIC in the .h, you should also create it in the .cpp as "int MyClass::Var".
    This was the error the compiler was finding, but the error message wasn't really clear and got me a lot of trouble to understand this.

  • In one topic I found that a STATIC attribute can be used by only one method. This is not true, and also gave me a lot of trouble.

  • Finally, I followed your reccomendation on not to use interruptions and used the SimpleTimer library that I downloaded. You also have to pass a handler when you initialize it, so I used all then tips above and it's now working fine.