我有一个带无源蜂鸣器的超声波距离传感器。无源蜂鸣器设置了不同的音调。蜂鸣器将一直播放,直到超声波距离传感器检测到任何障碍物。但是,Arduino 无法编译代码。它显示错误:
exit status 1
Error compiling for board Arduino/Genuino Uno.
这是完整的错误消息:
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.
我可以timer0_pin_port
在 Tone.cpp.o 中找到该函数。但我在 NewPing.cpp.o 中找不到相同的功能。
由于空间限制,我无法在此处发布 NewPing.cpp.o。您可以在此处下载 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);
}
预期:当距离传感器检测到任何项目时,蜂鸣器将停止播放。您必须使用音调方法来支持不同的音调。或者任何可以支持不同音调的类似功能。