Using New Ping library and TONE together

Hello,

I thought I would quickly throw together some code for varying a tone from the distance returned by a HC-SR04 Ultrasonic module.

I thought that simply mapping the returned distance would do it by using the NewPing library in conjunction with TONE. However I get an compilation error message:

Arduino: 1.0.6 (Windows 7), Board: "Arduino Mega 2560 or Mega ADK"
core.a(Tone.cpp.o): In function `__vector_13':

C:\Program Files\Arduino\hardware\arduino\cores\arduino/Tone.cpp:535: multiple definition of `__vector_13'

NewPing\NewPing.cpp.o:C:\Documents\Arduino\libraries\NewPing/NewPing.cpp:214: first defined here
c:/program files/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

I'm using the MEGA-2560. Is there a fundamental problem of using PWM's together? I can post my code if anyone wants to look?

Thanks.

code is:

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (cm).

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and max distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop()
{
delay(50); // Wait 50ms between pings.
unsigned int uS = sonar.ping_median(10); //multiple readings returned in microseconds.
Serial.print("Ping: ");
unsigned int dist = (uS / US_ROUNDTRIP_CM); // Convert ping time to cm and print result.
Serial.print(dist); //print the distance.
Serial.println("cm");

// map the distance range (in this case, 1cm to 300cm from the ultrasonic sensor.
// to the output pitch range (120 - 1500Hz)

unsigned int thisPitch = map(dist, 1, 300, 120, 1500); //map the distance.

tone(9, thisPitch, 10); // play the pitch:
delay(1); // delay in between reads for stability
}

There is a timer conflict between NewPing and Tone. See Google Code Archive - Long-term storage for Google Code Project Hosting.

The author has developed a NewTone library you can use.

Reading the HC SR04 is simple and there may not be a reason to use the NewPing library. The basic way of reading the HC SR04 is with pulseIn(). The pulseIn() function is "blocking" and if that creates problems with the tone, you can switch to interrupt based methods of reading the echo signal.

Thanks cattledog.... that's solved it!

cattledog. i have installed newping version 1.9.1 but still cant to use tone. how to use it? do you have an example. or reference for me? thanks before

i have installed newping version 1.9.1 but still cant to use tone. how to use it? do you have an example. or reference for me?

Read the references and find the links here https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

Timer 2 Conflict
Having a conflict with the tone library or another library using timer 2? Instead of the tone library, use my NewTone or toneAC libraries which instead uses timer 1 (and also has many other advantages). Or use my Timer Free Tone library which doesn't use any timers to generate tones.