Summary of libraries we're using, and what timers they use:
Servo.h: timers 5, 1, 4, and 3 (in that order, depending on how many servos you use)
- The Spider Redback documentation shows which timers are needed based on how many servos you use. It doesn't matter which pins you put the servos on.
- If you use 12 servos or fewer, it only really needs timer 5. In that case, you can use any normal PWM pins except 44, 45, and 46, which are those PWM pins associated with timer 5.
- Regardless of how many servos you use, the servo library still locks all four timers so that you cannot simultaneously use any other libraries that use those timers.
- This point is super confusing, so I'm going to make it again in different words. Each timer is associated with a set of pins. Those pins can be used for PWM (i.e., analogWrite) if the associated timer is not being used by something else. The servo library allocates for itself all four timers (5,1,4,3), which prevents you from loading up any other library that simultaneously that tries to allocate any of those timers. However, you can still use PWM on pins associated with timers that don't actually have servos attached to them.
Tone libraries:
- tone (built-in): timer 2. (Apparently uses more timers than that if you try to play more than one tone at the same time.) Any pins you want, though beware of PWM conflicts.
- NewTone: timer 1. Any pins you want, though beware of PWM conflicts.
- toneAC: timer 1. Pins 11 and 12 only, requires specialized wiring. Let's you control volume, which the other libraries don't.
- toneAC2:
timer 2. Any pins you want, though beware of PWM conflicts. Requires
specialized wiring. Gets you twice the volume, though can't control it.
We likely don't ever need to use this one.
NewPing (for ultrasonic sensor):
- timer 2 by default, but our
version (on Moodle site) sets the TIMER_ENABLED variable to false, which
makes it use no timers at all. It disables the capability for the
function ping_timer, but you only need that if you want to
try to do fancy multitasking-like behavior automatically. We can code
around that in other ways.
Timers and PWM pins: (see RedBack documentation and this summary as well)
- Timer 0: Pins 4 and 13
- Timer 1: Pins 11 and 12
- Timer 2: Pins 9 and10
- Timer 3: Pins 2, 3, 5
- Timer 4: Pins 6, 7, 8
- Timer 5: Pins 44, 45, 46
Why does this matter? Two reasons:
- You can't use two libraries at the same time that use the same timer
- If
you use a library with a timer that is associated with a set of pins
for PWM (e.g., Servo.h with timer 5, which is associated with pins 44-46
for PWM), beware of using those pins for a different device that
requires PWM, such as a dimmable LED. analogWrite uses PWM, and will
only work on a pin associated with a timer.
FAQ: What is the relationship between PWM (via analogWrite) and sending timing information to the servo libraries via the servo library?
- They're unrelated, as explained pretty well
in this
discussion. Servo commands use something that resembles PWM, and in the
abstract might be called PWM, but it's a very different format and timing than
the actual Arduino native PWM. It is sometimes instead referred to as PPM.
- Here's the difference: PWM (pulse width management) changes the width of a pulse. PPM (pulse position management) changes the time when a pulse starts relative to a fixed start time.
- So there's no connection between the two, and
you don't need PWM pins to handle servos. BUT, it is the case that the servo
software uses the built-in timers in order to generate the PPM signals that it
needs, and so when using those timers for servos, the pins that those timers
co-exist with can't be used for PWM (e.g, analogWrite).