Servo.h and own timer

I have some code set up on a Mega that uses Timer 1. I'm trying to now add in the servo library (Servo.h) but it's throwing an error "multiple definition of '_vector_17'". I changed my code over to Timer 3 and the error just moved to "vector_32".

Is the Servo library just too greedy with taking control of all the timers on the board that it's a non-starter if I have my own timers in the code already or is there a way around this?

Appreciate the help!

No. It uses timer 1. There is a servo library available that uses timer 2 cunningly named ServoTimer2

GitHub - nabontra/ServoTimer2: ServoTimer2 is a simple library for Arduino 1.x that does not use Timer1 in case of a conflict.

I read that it uses Timer 5 on the Mega, but in any case it doesn't matter what timer I am using with my code, Servo.h conflicts with it. Is it really the case that in code that already uses any of the 16 bit timers that it's impossible to use this library?

Maybe you should edit "ServoTimers.h" in the Servo library. It looks like it enables uses of 5, 1, 3, and 4, by default. Remove one of those from the lists to reserve it for your use.

// Say which 16 bit timers can be used and in what order
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define _useTimer5
#define _useTimer1
#define _useTimer3
#define _useTimer4
typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t;

Find the file in:
(Arduino program directory)/libraries/Servo/src/avr/ServoTimers.h

1 Like

You could well be right. I am more used to the clash of timers query occurring in relation to the Uno and its close relations

In any case, I would not expect that a Mega running only the Servo library would use all of the available timers

Editing "ServoTimers.h" did the trick. Odd design choice in the library to stake a claim to every 16bit timer on the board, but at least it was a simple fix.

Thanks @johnwasser for the guidance.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.