I am just trying to use an ultrasonic sensor with a buzzer but the buzzer doesn't respond to my code. I mean it doesn't make any sound. I tested it to check if by sending 5 volts to it, it will make a sound.. and it did... but using this code below... for some reason, it doesn't make any sound....
#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define buzzer 13
NewPing sonar1 (TRIGGER_PIN, ECHO_PIN);
long inches1;
int sound = 250;
void setup (){
Serial.begin (9600);
pinMode (TRIGGER_PIN, OUTPUT);
pinMode (ECHO_PIN, INPUT);
pinMode (buzzer, OUTPUT);
}
void loop () {
inches1 = sonar1.ping_in();
if (inches1 <= 5 ){
// sound = 250;
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000);
Serial.print (inches1);
Serial.print ('\n');
}
delay (200);
}
-
2don't use tone() if it makes sound with 5 V. only set pin HIGHJuraj– Juraj ♦2019年09月20日 18:45:26 +00:00Commented Sep 20, 2019 at 18:45
-
hhhmmmm..... i see... i will try.. thanks for your answer... :)Glenn Macion– Glenn Macion2019年09月20日 18:48:57 +00:00Commented Sep 20, 2019 at 18:48
-
what exit code are you talking about?jsotola– jsotola2019年09月20日 20:12:11 +00:00Commented Sep 20, 2019 at 20:12
-
1What's the whole message the Arduino IDE gives you. Exit 1 usually means you've got a missing library or a syntax error in your code.Dougie– Dougie2019年09月21日 10:22:00 +00:00Commented Sep 21, 2019 at 10:22
1 Answer 1
If you have a look at the complete error message you will see that there is a collision between the libraries NewPing and Tone.
Arduino: 1.8.9 (Windows Store 1.8.21.0) (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.
answered Sep 21, 2019 at 12:26
-
1yes, but the point is that they don't have to use tone()2019年09月21日 18:20:43 +00:00Commented Sep 21, 2019 at 18:20
-
I agree! But the question is "error" and perhaps someone take in the information that libraries can have conflicts, I have seen it to many times.MatsK– MatsK2019年09月24日 17:55:21 +00:00Commented Sep 24, 2019 at 17:55
lang-cpp