In function `SoftwareSerial::read()': (.text+0x0): multiple definition of `__vector_9'

Hello there,
I am working on combining three codes(RC, HRSC04, GPS), where Remote Control and ultrasonic codes combining didn't give me any problem as it worked (named it as HRSC04_RC). But, adding GPS code to the HRSC04_RC giving me an error. I believe Software serial and enable interrupt libraries won't work together.

Can anyone give me the solution to it, how to avoid the error?


#include <EnableInterrupt.h>
long loop_timer, tiempo_ejecucion;

volatile long contPotenciaInit; //  RC POTENCIA
volatile int PulsoPotencia;

volatile long contPitchInit; // RC PITCH
volatile int PulsoPitch;

volatile long contRollInit; //  RC ROLL
volatile int PulsoRoll;

volatile long contYawInit; //  RC YAW
volatile int PulsoYaw;

//HRSC04/////
float altura, alturaFilt;
bool initHC = true;

volatile float PulsoAlt;
volatile long contAltInit;

///GPS ///
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

// Choose two Arduino pins to use for software serial
int RXPin = 7;
int TXPin = 8;

int GPSBaud = 115200;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);



void setup() {
  
  ///HRSC_04///////
  pinMode(18, INPUT_PULLUP); // Echo
  pinMode(A3, OUTPUT);      // Trg
  enableInterrupt(18, INTalt, CHANGE);

  //RC///////////

   pinMode(21, INPUT_PULLUP);                  // YAW
  enableInterrupt(21, INTyaw, CHANGE);
  pinMode(20, INPUT_PULLUP);                  // POTENCIA
  enableInterrupt(20, INTpotencia, CHANGE);
  pinMode(3, INPUT_PULLUP);                 // PITCH
  enableInterrupt(3, INTpitch, CHANGE);
  pinMode(2, INPUT_PULLUP);                  // ROLL
  enableInterrupt(2, INTroll, CHANGE);

  /////GPS SET UP/////
  
  gpsSerial.begin(GPSBaud); // Start the software serial port at the GPS's baud

  Serial.begin(115200);
}
////RC INTERRUPTS///

void INTyaw() {
  if (digitalRead(21) == HIGH) contYawInit = micros();
  if (digitalRead(21) == LOW) PulsoYaw = micros() - contYawInit;
}

void INTpotencia() {
  if (digitalRead(20) == HIGH) contPotenciaInit = micros();
  if (digitalRead(20) == LOW) PulsoPotencia = micros() - contPotenciaInit;
}

void INTpitch() {
  if (digitalRead(3) == HIGH) contPitchInit = micros();
  if (digitalRead(3) == LOW) PulsoPitch = micros() - contPitchInit;
}

void INTroll() {
  if (digitalRead(2) == HIGH) contRollInit = micros();
  if (digitalRead(2) == LOW) PulsoRoll = micros() - contRollInit;
  }

 ///////HR-SC04////
  void INTalt() {
  if (digitalRead(18) == HIGH) contAltInit = micros();
  if (digitalRead(18) == LOW) {
    PulsoAlt = micros() - contAltInit;
    initHC = true;
  }
}

void loop() {
  while (micros() - loop_timer < 6000);
  loop_timer = micros();
  HCSR04();
  Serial.println(alturaFilt);

  /////RC///////
  while (micros() - loop_timer < 10000);

  tiempo_ejecucion = (micros() - loop_timer) / 1000;
  loop_timer = micros();

  Serial.print(PulsoPotencia);
  Serial.print("\t");
  Serial.print(PulsoPitch);
  Serial.print("\t");
  Serial.print(PulsoRoll);
  Serial.print("\t");
  Serial.println(PulsoYaw);

//GPS LOOP//

  // This sketch displays information every time a new sentence is correctly encoded.
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();

}

 ///////HR-SC04////

void HCSR04() {
  if (initHC == true) {
    alturaFilt = PulsoAlt * 0.01715; 
    alturaFilt = alturaFilt * 0.8 + PulsoAlt * 0.01715 * 0.2;  
    analogWrite(A3, 255);  //  A3 HIGH
    delayMicroseconds(10); 
    analogWrite(A3, 0);    //  A3 LOW
    initHC = false;        
  }
}

//////gps diplay info/////

void displayInfo()
{
  if (gps.location.isValid())
  {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
    
  }
  else
  {
    Serial.println("Location: Not Available");
  }

   Serial.println();
  Serial.println();
  delay(1000);
}

After compiling, it says:

E:\temp\arduino_build_848242\libraries\SoftwareSerial\SoftwareSerial.cpp.o (symbol from plugin): In function `SoftwareSerial::read()':
(.text+0x0): multiple definition of `__vector_9'
E:\temp\arduino_build_848242\sketch\hr_sco4_new_sketch_try_with_gps.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
E:\temp\arduino_build_848242\libraries\SoftwareSerial\SoftwareSerial.cpp.o (symbol from plugin): In function `SoftwareSerial::read()':
(.text+0x0): multiple definition of `__vector_10'
E:\temp\arduino_build_848242\sketch\hr_sco4_new_sketch_try_with_gps.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
E:\temp\arduino_build_848242\libraries\SoftwareSerial\SoftwareSerial.cpp.o (symbol from plugin): In function `SoftwareSerial::read()':
(.text+0x0): multiple definition of `__vector_11'
E:\temp\arduino_build_848242\sketch\hr_sco4_new_sketch_try_with_gps.ino.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 Mega or Mega 2560.

Thank you!

Why are you using software serial, the Mega has 3 spare hardware serial ports ?

Hi @srnet,
If I understand you correctly, should I completely take off software serial from the code and just give digital input to GPS and read the data from it?

Connect the GPS up to one of the hardware serial ports, Serial1, Serial2 or Serial3.

You would then read the GPS with something like;

 while (Serial1.available() > 0)
    if (gps.encode(Serial1.read()))

Thanks @srnet
I tried connecting to serial2 (16, 17), and still, I'm not able to compile the code.

Don't you think it would be helpful to post the complete, updated code?