Pages: [1]   Go Down
Author Topic: Using HC-SRO4 distance module and generating tone at the same time?  (Read 648 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm trying to create a project that would involve using the distance module (HC-SRO4) as well as a speaker to generate sound. However, I run into some trouble when compiling whenever I try to use both at once. It will give me this error:

core.a(Tone.cpp.o): In function `__vector_7':
C:\Users\Drew\Desktop\arduino-1.0.4\hardware\arduino\cores\arduino/Tone.cpp:535: multiple definition of `__vector_7'
NewPing\NewPing.cpp.o:C:\Users\Drew\Desktop\arduino-1.0.4\libraries\NewPing/NewPing.cpp:214: first defined here

If I have only the code for the distance module, it compiles fine. If I have only the code for generating tone, it compiles fine. But when ! try to combine the two it won't let me. Does anybody know why this may be?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think it means that both libraries are trying to use the same interrupt.  When I look in the datasheet "Vector Number 7" is the watchdog timer vector.  Maybe it means the vector at address 7.  That would be the Timer 2 Compare Register A interrupt.  That makes more sense.

I guess the two libraries are not compatible. smiley-sad
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The rangefinder really doesn't need a library. This code is from my book 'Hacking Electronics' p 155.

Code:
// range_finder_budget
// Arduino Uno or Leonardo

int trigPin = 9;
int echoPin = 10;
int gndPin = 11;
int plusPin = 8;

int lastDistance = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(gndPin, OUTPUT);
  digitalWrite(gndPin, LOW);
  pinMode(plusPin, OUTPUT);
  digitalWrite(plusPin, HIGH);
}

void loop()
{
  Serial.println(takeSounding_in());
  delay(500);
}

int takeSounding_cm()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  int duration = pulseIn(echoPin, HIGH);
  int distance = duration / 29 / 2;
  if (distance > 500)
  {
    return lastDistance;
  }
  else
  {
    lastDistance = distance;
    return distance;
  }
}

int takeSounding_in()
{
  return takeSounding_cm() * 2 / 5;
}
Logged

--
My New Arduino Book: http://www.arduinobook.com

Pages: [1]   Go Up
Jump to: