HI,
I'm building a Sonar sensor box that provides a reward once the participant steps back 6 feet to teach distance. I keep getting errors that tell me:
Arduino: 1.8.19 (Windows Store 1.8.57.0) (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.
Don't know what is causing this. Any assistance would be appreciated!
Coding below:
/*Sonic Sensor = D2 (Trig), D3 (echo), POS, NEG
*Servo = D9, 5V, NEG
*Speaker = D10, NEG
*LCD = 5V, GND, A4, A5
*Green LED = D12, GRD (with 220ohm resister)
*Red LED = D13, GRD (with 220ohm resister)
*
*
*
*
*
*
*
*/
#include <LiquidCrystal_I2C.h>
#include <Servo.h> //Needed to control servo
#include <NewPing.h>
#define TRIG_PIN 2
#define ECHO_PIN 3
#define SERVO_PIN 9
#define RED_LED_PIN 13
#define GREEN_LED_PIN 12
#define SPEAKER_PIN 10
#define MAX_DISTANCE 5000 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
const int maxDistance = 400; // cm
const int rewardDistance = 180; // cm (6 feet)
const int rewardSound[] = {440, 880, 587, 784}; // Short melody for reward
Servo servo;
LiquidCrystal_I2C lcd (0x27, 20, 4); //Initiate LCD
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance
void setup() {
lcd.begin();
lcd.backlight();
servo.attach(SERVO_PIN);
servo.write(0);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(SPEAKER_PIN, OUTPUT);
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED initially
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED initially
lcd.clear();
lcd.print("Welcome to Social Distance!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Now GO AWAY!!!");
delay(2000);
}
void loop() {
delay(2000); // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
long duration = getDistance();
int distanceInCm = duration * 0.034 / 2;
int feet = distanceInCm / 30.48; // Convert cm to feet
int inches = distanceInCm - (feet * 30.48); // Calculate remaining inches
lcd.clear();
lcd.print("Distance: ");
lcd.print(feet);
lcd.print(" ft ");
lcd.print(inches);
lcd.print(" in");
if (distanceInCm >= rewardDistance) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Congratulations!");
lcd.setCursor(0, 2);
lcd.print("You reached the Social Distance!");
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green LED
servo.write(90); // Move servo 90 degrees
delay(2000); // Hold reward position for 2 seconds
servo.write(0); // Move servo back to initial position
playRewardSound(); // Play reward melody
} else {
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED
}
}
long getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return duration;
}
void playRewardSound() {
for (int i = 0; i < 4; i++) {
tone(SPEAKER_PIN, rewardSound[i], 250);
delay(100);
}
}