I would like to use the Radiohead library and control a servo from the same Leonardo board.
Now the Radiohead library occupies timer1
and therefore cant be used together with the servo
library. (It does offer to use timer2
which the Leonardo does not have.)
Are there any other Servo libraries that use timer3
or timer4
which the Leonardo has?
I have already tried:
- https://github.com/nabontra/ServoTimer2 does not compile
- https://www.pjrc.com/teensy/td_libs_TimerOne.html does not work (calling function blocks)
3 Answers 3
I wonder if you could use this PWM version of the Servo library written for the Arduino MEGA? Looking at the code, it looks like you would have to remove the "else if (pin == 44)" and "else if (pin == 45)" blocks of code in the attach() and detach() functions in Update Servo_Hardware_PWM.cpp. Just guessing here. I haven't tried it out.
https://github.com/dadul96/Arduino-Servo-Hardware-PWM-Library
-
Comment from an anon user:
Sorry, this won't work. I checked the datasheet for the ATMega32u4 and Timers 3 and 4 can't be used to change the state of a pin. So no, hardware servo control using the above code on a Leonardo.
Greenonline– Greenonline2019年05月09日 07:36:03 +00:00Commented May 9, 2019 at 7:36
You could try manually sending the servo pulses with something like:
digitalWrite(SERVOPIN,HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(SERVOPIN,LOW);
As long as the duration of the pulses is consistent, it doesn't matter if they don't occur regularly.
Downsides will be that there may be jitter if there are interrupts running, although there is the advantage that this will work on just about any pin.
If you are looking for a simple proof of concept, this may enough to test it out.
In file ServoTimers.h
in standard Arduino Servo library change the timer
#elif defined(__AVR_ATmega32U4__)
#define _useTimer3
typedef enum { _timer3, _Nbr_16timers } timer16_Sequence_t;
on Windows the file ServoTimers.h
is located in C:\Program Files (x86)\Arduino\libraries\Servo\src\avr
-
thanks, but this leads me to the compile error described here: forum.arduino.cc/index.php?topic=527356.0clamp– clamp2019年05月16日 19:03:16 +00:00Commented May 16, 2019 at 19:03
-
you get that error only if you don't change the timer number to 3 as I write in the Answer2019年05月16日 19:15:05 +00:00Commented May 16, 2019 at 19:15
-
oki fixed the compile error. now the problem is that as soon as the servo is connected (pin 5) the radiohead library seems to stop working.clamp– clamp2019年05月16日 19:19:50 +00:00Commented May 16, 2019 at 19:19
-
could it be a powering problem?2019年05月16日 19:31:28 +00:00Commented May 16, 2019 at 19:31
-
could be, i will try to run it with external power supply and not the usb.clamp– clamp2019年05月16日 19:35:14 +00:00Commented May 16, 2019 at 19:35