NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

crispy:
This may have been answered before in the thread, but I didn't see it on a quick read-through.

I'm using the NewPing library (which is awesome) and the Arduino Motor Shield. Unfortunately the shield uses pins 3 and 11 to do the motor PWM, which conflicts with Timer2 used in NewPing.

Is there a way to change what timer NewPing uses? Or, is it just simpler to un-stack the shield and the Arduino and manually wire the PWM into different pins?

First off, NewPing does not use Timer2 for the standard ping() and ping_median() methods. It's only for the timer interrupt ping_timer() method where NewPing uses Timer2 on the Arduino Uno. Also, the library uses Timer4 for the Teensy and Leonardo (ATmega32U4). So, if you're just using ping() or ping_median(), you have no conflict with Timer2 (or Timer4 on the ATmega32U4).

However, if you're using the timer interrupt ping_timer() method, yes, it will use Timer2 and there isn't an option to use Timer1. I had first written it to work with both Timer1 and Timer2 but decided to only implement Timer2 because the servo library uses Timer1, which is a common library. However with motors, there's many different ways of doing it, most of which don't use any timers (I use serial motor controllers for example).

Also, because Timer2 is 8 bit and Timer1 is 16 bit, it's not as simple as changing all the 2's to 1's to make it work. It wouldn't be too difficult for someone to modify the code to allow a switch for Timer2 or Timer1. But, they would need a good deal of knowledge on how the timer interrupts work on the ATmega at a lower level. I don't feel it's worth doing as I would think most people writing event-driven code that requires using the ping_timer() method also wouldn't be using the motor shield and instead using an outboard motor controller with better PWM control. But, maybe I'm mistaken. In any case, I'm probably not the one to add this support, someone else will if they feel it's required for their situation.

Tim