I am using an Arduino Uno R3, ultrasonic sensors and an LCD screen basically to create a reversing sensor from scratch. I am part way through prototyping my design and I am evolving the code as I go, I have just included a passive buzzer to give the tone when near an object but when I try to verify or upload the code it gives me an error I have no idea how to resolve. I will include both the full code and the error, any help would be VERY gratefully appreciated!
//include relevant libraries
#include <NewPing.h>
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Define pins for US Sensors using single pin method
#define PING_PIN_A 13
#define PING_PIN_B 12
#define PING_PIN_C 11
//Set max distance required (in cm) for sensors, max distance is approx.400-500cm
#define MAX_DISTANCE 99
//set LCD serial address
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int i2c_addr = 0x27;
//Set delay time for buzzer tone
int dt_long1 = 100;
int dt_long2 = 150;
int dt_short1 = 25;
int dt_short2 = 50;
//Set buzzer pin
int buzzerPin = 10;
//set threshold for buzzer
const int threshold_short = 20;
const int threshold_long = 50;
//set unsigned integers for US echo times
unsigned int uS_A = 0;
unsigned int uS_B = 0;
unsigned int uS_C = 0;
unsigned int dist_A = 0;
unsigned int dist_B = 0;
unsigned int dist_C = 0;
//Using NewPing library setup pin and max distance
NewPing sonar_A(PING_PIN_A, PING_PIN_A, MAX_DISTANCE);
NewPing sonar_B(PING_PIN_B, PING_PIN_B, MAX_DISTANCE);
NewPing sonar_C(PING_PIN_C, PING_PIN_C, MAX_DISTANCE);
//call voids for use in loop
void Sensor1();
void Sensor2();
void Sensor3();
void DeBug();
//Setup
void setup() {
//start serial monitor at 115200 baud to see US outut and for debugging
Serial.begin(115200);
// initialize the lcd and turn on backlight
lcd.init();
lcd.init();
lcd.backlight();
//Buzzer pin output
pinMode(buzzerPin, OUTPUT);
tone(buzzerPin, 1000, 2000);
}
//start loop
void loop() {
Sensor1();
Sensor2();
Sensor3();
DeBug();
lcd.setCursor(0, 0);
lcd.print("d_A d_B d_C");
lcd.setCursor(0, 1);
lcd.print(dist_A);
lcd.print("cm");
lcd.setCursor(6, 1);
lcd.print(dist_B);
lcd.print("cm");
lcd.setCursor(12, 1);
lcd.print(dist_C);
lcd.print("cm");
}
//void containing sensor instructions
void Sensor1() {
static unsigned long Sensor1Millis = millis(); //by setting this as static it is called only once and the moves outside of void.
if (millis() - Sensor1Millis > 100) {
unsigned int uS_A = sonar_A.ping();
Sensor1Millis = millis();
dist_A = (uS_A / US_ROUNDTRIP_CM);
if (dist_A < threshold_long) {
tone(buzzerPin, 440); // A4
delay(dt_long1);
tone(buzzerPin, 494); // B4
delay(dt_long1);
}
else {
digitalWrite(10, LOW);
}
delay(200);
if (dist_A < threshold_short) {
tone(buzzerPin, 659); // E4
delay(dt_short1);
tone(buzzerPin, 698); // F4
delay(dt_short1);
}
else {
digitalWrite(10, LOW);
}
delay(200);
}
}
void Sensor2() {
static unsigned long Sensor2Millis = millis();
if (millis() - Sensor2Millis > 100) {
unsigned int uS_B = sonar_B.ping();
Sensor2Millis = millis();
dist_B = (uS_B / US_ROUNDTRIP_CM);
if (dist_B < threshold_long) {
tone(buzzerPin, 440); // A4
delay(dt_long1);
tone(buzzerPin, 494); // B4
delay(dt_long1);
}
else {
digitalWrite(10, LOW);
}
delay(200);
if (dist_A < threshold_short) {
tone(buzzerPin, 659); // E4
delay(dt_short1);
tone(buzzerPin, 698); // F4
delay(dt_short1);
}
else {
digitalWrite(10, LOW);
}
delay(200);
}
}
void Sensor3() {
static unsigned long Sensor3Millis = millis();
if (millis() - Sensor3Millis > 100) {
unsigned int uS_C = sonar_C.ping();
Sensor3Millis = millis();
dist_C = (uS_C / US_ROUNDTRIP_CM);
if (dist_B < threshold_long) {
tone(buzzerPin, 440); // A4
delay(dt_long1);
tone(buzzerPin, 494); // B4
delay(dt_long1);
}
else {
digitalWrite(10, LOW);
}
delay(200);
if (dist_A < threshold_short) {
tone(buzzerPin, 659); // E4
delay(dt_short1);
tone(buzzerPin, 698); // F4
delay(dt_short1);
}
else {
digitalWrite(10, LOW);
}
delay(200);
}
}
void DeBug() {
Serial.print(dist_A);
Serial.print(dist_B);
Serial.println(dist_C);
Serial.println(threshold_long);
Serial.println(threshold_short);
}```
Error is below.
Arduino: 1.8.19 (Windows 10), Board: "Arduino 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 Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.