简体   繁体   English

带有无源蜂鸣器的Arduino超声波距离传感器可实现不同的音调

[英]Arduino Ultrasonic Distance Sensor with Passive Buzzer to achieve different tones

I have an Ultrasonic Distance Sensor with Passive Buzzer. 我有一个带被动蜂鸣器的超声波距离传感器。 The passive buzzer was set up with different tones. 被动蜂鸣器设置有不同的音调。 The buzzer will keep playing until the Ultrasonic Distance Sensor detect any obstruction items. 蜂鸣器将一直播放,直到超声波距离传感器检测到任何障碍物为止。 However, the Arduino couldn't compile the code. 但是,Arduino无法编译代码。 it displays the error: 它显示错误:


exit status 1
Error compiling for board Arduino/Genuino Uno.

Here is the full error message: 这是完整的错误消息:

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"

Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':

(.text+0x0): multiple definition of `__vector_7'

libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I could locate the function timer0_pin_port in Tone.cpp.o. 我可以在timer0_pin_port中找到函数timer0_pin_port But I couldn't find the same function in NewPing.cpp.o. 但是我在NewPing.cpp.o中找不到相同的功能。

Because of the space limit, I counldn't post the NewPing.cpp.o here. 由于篇幅所限,我无法在此处发布NewPing.cpp.o。 You can download the NewPing.cpp.o here: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home Tone.cpp.o is the original document in the library. 您可以在此处下载NewPing.cpp.o: https: //bitbucket.org/teckel12/arduino-new-ping/wiki/Home Tone.cpp.o是该库中的原始文档。

#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 10 // Maximum distance we want to ping for (in centimeters). 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

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

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  if (sonar.ping_cm() == 0)
  tone(2,4000);
  else 
  tone(2,0);
}

Expected: The Buzzer will stop playing when the DIstance sensor detects any items. 预期:当DIstance传感器检测到任何物品时,蜂鸣器将停止播放。 You have to use tone method to support different Tones. 您必须使用音调方法来支持不同的音调。 Or any similar functions that can support different tones. 或任何可以支持不同音调的类似功能。

From what I understand that the Tone and the NewPing library have a conflicting use of the same interrupt __vector_7 . 据我了解, ToneNewPing库对同一中断__vector_7使用存在冲突。 NewPing is known to have conflicting issues, I would suggest you use the original ping in Arduino. 众所周知,NewPing存在冲突问题,我建议您使用Arduino中的原始ping。 Here's a comprehensive example for it. 这是一个综合的例子

If you're certain that you're not using the ping_timer() method then in the NewPing.h file make TIMER_ENABLED to false . 如果确定没有使用ping_timer()方法,则在NewPing.h文件TIMER_ENABLEDfalse

Here's a link which talks about Multiple Definition of "__vector_7" Error further. 这是一个进一步讨论“ __vector_7”错误的多重定义的链接。

Here's the thread of a similar problem on the arduino forum . 这是arduino论坛上类似问题的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM