Is it possible to use more than one timer on an Arduino Uno?

4.4k views Asked by At

I know that the Arduino has three timers (timer0, timer1, timer2) and one is 16bit and the other is 8 bit but i am not sure how to use them and if possible can i use all three in the same project with each one having different time intervals that should never collide. i assume that i need a library to call them but i don't know which one. All the timer would run functions and not interact with Pins directly and the shortest time interval will be set at 1/3 of a second and the longest time interval will vary from 2 seconds to 0.5 seconds. If anyone can give me an example of a sketch with three timers i could probably get a feel for what i need to do.

2

There are 2 answers

1
Sorin On

You can run a single timer at an interval that is the lowest common denominator of your intervals.

To do this You need to have a heap storing the events you want to trigger. Then whenever you insert or trigger an event, you update the timer by looking at the top of the heap to pick up the earliest event you want. If that is sooner than whatever is running, or there's nothing running you update the timer to trigger the new event. When an event triggers, you remove it from the heap. If you want to trigger periodically you can add the event back in the heap with a new 'time' that is now() + period.

0
Damago On

Short answer is: TimerThree is not available for Arduino UNO! It uses Atmega328 CPU which contains only: Timer0 (used internally by Arduino code for millis()), Timer1 which is 16 bit timer and usable, and Timer2 which is an 8bit timer.

The TimerThree is available only on ATMEGA2560 or similiar chips, so only on Arduino MEGA.

There on the page where TimerThree is included there is a completely wrong description: firstly there is no clear statement that TimerThree will not work with UNO, and to make it worse there is a statement that the TimerThree library was 'Modified March 2009 by Jérôme Despatis and Jesse Tane for ATmega328 support' which is clear nonsense.