URGENT assignment help! Using Servo and Tone libraries.

I am creating a sensor device that utilises libraries (Servo, NewTone, NewPing, SoftwareSerial). I have researched everywhere on the internet, but nothing refers to my problem. I would like to use the Servo and NewTone libraries, though they get an error when I use them both. When I remove one of them the code works and vice versa with the other library.

The error code I get is:

"C:\Users\LeTran\AppData\Local\Temp\arduino_build_906327/..\arduino_cache_902643\core\core_arduino_avr_mega_cpu_atmega2560_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\Users\LeTran\AppData\Local\Temp\arduino_build_906327" -lm
C:\Users\LeTran\AppData\Local\Temp\arduino_build_906327\libraries\NewTone\NewTone.cpp.o (symbol from plugin): In function `NewTone(unsigned char, unsigned long, unsigned long)':

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

C:\Users\LeTran\AppData\Local\Temp\arduino_build_906327\libraries\Servo\avr\Servo.cpp.o (symbol from plugin):(.text+0x0): first defined here

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

collect2.exe: error: ld returned 1 exit status

Using library Servo at version 1.1.2 in folder: C:\Program Files (x86)\Arduino\libraries\Servo 
Using library NewTone in folder: C:\Users\LeTran\Documents\Arduino\libraries\NewTone (legacy)
Using library NewPing in folder: C:\Users\LeTran\Documents\Arduino\libraries\NewPing (legacy)
Using library SoftwareSerial at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial 
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

The code I am using is:

#include <Servo.h>
#include <NewTone.h>
#include "NewPing.h"
#include "SoftwareSerial.h"

#define BT_SERIAL_TX 19
#define BT_SERIAL_RX 18

#define TRIGGER_PIN  10
#define ECHO_PIN     11
#define MAX_DISTANCE 400

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);

char blueToothVal;           //value sent over via bluetooth
char lastValue;              //stores last state of device (on/off)

float duration, distance;

const int inputPin = 7;
const int ledPin = 4;
const int buzzerPin = 3;
const int buttonPin = 1;
const int buttonPinn = 0;

int pirState = LOW;
int lastpirState = 0;
int pirCounter = 0;
int iterations = 5;
int state = LOW;
int buttonState = HIGH;
int lastButtonState = HIGH;
int reading;
int btval = LOW;

int staten = LOW;
int buttonStaten = HIGH;
int lastButtonStaten = HIGH;
int readingn;

Servo My_servo;
int trig = 6;
int vcc = 7;
int gnd = 4;
int echo = 5;
float min_val = 0;
float ultra_distance[90] = {0};
int angle[91] = {0};

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  My_servo.attach(9  , 600, 2300);
  My_servo.write(600);
  pinMode(trig, OUTPUT);
  pinMode(gnd, OUTPUT);
  pinMode(vcc, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trig, LOW);
  digitalWrite(gnd, LOW);
  digitalWrite(vcc, HIGH);
  digitalWrite(echo, LOW);
  
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(inputPin, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPinn, INPUT);
  BluetoothSerial.begin(9600);
  }

void loop() {
  {
  pirState = digitalRead(inputPin);

  button();
  buttonn();
  sweep();{
  if(BluetoothSerial.available())
  {//if there is data being recieved
    blueToothVal=BluetoothSerial.read(); //read it
  }
  if (blueToothVal=='n')
  {//if value from bluetooth serial is n
    btval == !btval;
    if (lastValue!='n'){
      BluetoothSerial.println("the min value ="); //print LED is on
      BluetoothSerial.println(min_val);}
      
    lastValue=blueToothVal;
  }
  else if (blueToothVal=='f')
  {//if value from bluetooth serial is n
    btval == LOW;    
    if (lastValue!='f'){
      BluetoothSerial.println(F("Sweep is LOW"));}
    lastValue=blueToothVal;
  }
  delay(1000);
  }

  if (pirState != lastpirState) {
    if (pirState == HIGH) {
      pirCounter++;
      BluetoothSerial.println("on");
      BluetoothSerial.print("number of passes: ");
      BluetoothSerial.print(pirCounter);
      if(state == HIGH){
        digitalWrite(ledPin, HIGH);
        BluetoothSerial.println("    LED on");
      }
        if(state == LOW){
        digitalWrite(ledPin, LOW);
        BluetoothSerial.println("    LED off");
      }
        if(staten == HIGH){
        NewTone(buzzerPin, 1000);
        BluetoothSerial.println("    Buzzer on");
      }
        if(staten == LOW){
        NewTone(buzzerPin, 0);
        BluetoothSerial.println("    Buzzer off");
      }
      
      while (digitalRead(inputPin) == HIGH) {
    ultrasonic();
  }
    } else {
      BluetoothSerial.println("off");
      digitalWrite(ledPin, LOW);
      NewTone(buzzerPin, 0);
    }
    delay(100);
  }
  lastpirState = pirState;
}

}

void ultrasonic () {
  
  duration = sonar.ping_median(iterations);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
  BluetoothSerial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    BluetoothSerial.println("Out of range");
  }
  else {
    BluetoothSerial.print(distance);
    BluetoothSerial.println(" cm");
    delay(500);
  }
  delay(500);
}

void button (){
    reading = digitalRead(buttonPin);
   if (reading != lastButtonState) {
      lastDebounceTime = millis();
      lastButtonState = reading;
   } 

   if ((millis() - lastDebounceTime) > debounceDelay) {
       if (buttonState != lastButtonState) {
           buttonState = lastButtonState;
           if (buttonState == HIGH) {
                 state = !state;
           }
       }
   }
}

void buttonn (){
    readingn = digitalRead(buttonPinn);
   if (readingn != lastButtonStaten) {
      lastDebounceTime = millis();
      lastButtonStaten = readingn;
   } 

   if ((millis() - lastDebounceTime) > debounceDelay) {
       if (buttonStaten != lastButtonStaten) {
           buttonStaten = lastButtonStaten;
           if (buttonStaten == HIGH) {
                 staten = !staten;
           }
       }
   }
}

void sweep(){
  
{ My_servo.write(0);
  delay(600);
  int j = 0;
  int index = -2; // check index value

  for (int i = 0; i <= 100; i += 1)
  {
    My_servo.write(i);
    delay(50);
    angle[j] = i;
    ultra_distance[j] = measure_distance_cm();
    delay(20);
    j += 1;
    if (i == 90) {
      break;
    }
  }
  for (j = 0; j <= 90; j += 1)
  { min_val = max(min_val, ultra_distance[j]);

  }

  for (j = 0; j <= 90; j += 1)
  { if (min_val > ultra_distance[j])
    {
      min_val = ultra_distance[j];
      index = j;
    }

  }
  My_servo.write(angle[index]);
  delay(1000);
  while (true)
  {
    float new_val = 0;

    new_val = measure_distance_cm();
    if (abs(ultra_distance[index] - new_val) >= 2.0)
    { break;
    }
  }


}
}


  float measure_distance_cm(){
  float distance = 0;
  long time_value = 0;
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  time_value = pulseIn(echo, HIGH);
  distance = .034 * time_value / 2;
  return distance;
}

Servo and Tone libraries are probably both trying to use the same hardware timer of the chip.

What board are you using?

I am using the Arduino Mega 2560.

Oh, youre right, the software serial probably isnt needed. But, how do i check for the timers. Do I go into the each of the library's ___.h files? Do timers refer to pins on the board? How do I know which pins to use?

Thank you very much for the swift replies. I'l try to follow your instructions and then report back :p.

I've tried to understand the timers, and pins. Though, how do you make the servo or the NewTone library to use a different timer?

There is a servo library called ServoTimer2.h. Is that giving you any hints?

Steve

Is this assignment urgent because you left it too late to start your homework?

...R

It was a group project and I had to cover for another person. I guess you could say I left it too late.

Assignment is over, but I'm still curious about how to do this.

I've tried to look into the .h and .cpp files. But j have had no luck. Tomorrow I'll look into comparing the servo and servo timer 2.

Though, the servo should be timer 5 since I'm using the arduino mega 2560. Which leads me to think it might not be the timer??

Literacy. If you don't know a term in a program then look it up until it makes logical sense or you have found a bug.

Do not skip over unknowns when troubleshooting.
Own the code or you can be trying to fix a problem by changing the wrong lines of code, write more bugs than you fix.

Google is your friend.