Multiple Definition Error (Vector 47)

Hi, I'm very new on Arduino and i'm trying to combine of 2 codes. This code is not give errors for Arduino Leonardo (I just compiled for Leonardo and it was done, i don't have Leonardo and it has different pins) but it gives errors for Arduino Mega 2560. The error is called "Multiple Definition Error" for Vector 47.

I have a robot, that robot has 4 wheels and different functions. (Sorry, but my English is not very good) I can command to this robot on phone. So, this code has bluetooth codes and movement codes.

That's all i can say. Where is my false?

So, this my code, also i attached that code.

#include <AltSoftSerial.h>
AltSoftSerial BTserial;

#include <Servo.h>
int aci=90,i;
Servo myservo;
int gecikme=40;

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Pins de HC-SR04
const int trigger_sol = 10;
const int echo_sol = 11;

// Pins de HC-SR04
const int trigger_sag = 12;
const int echo_sag = 13;

// Constants for hardware
const byte m_PIN[] = {23,25,27,29};
const byte SWITCH_PIN[] = {31,33,35,37};

// general variables
boolean m_State[] = {false,false,false,false};
boolean switch_State[] = {false,false,false,false};
boolean oldswitch_State[] = {false,false,false,false};

const int on_sol = 22;
const int on_orta = 24;
const int on_sag = 26;

const int hareket_sensoru_sol = 51;
const int hareket_sensoru_sag = 53;

int durum;
int engel_oku;

void setup() {

Serial.begin(9600);
myservo.attach(10);
// Configuración de los pines
pinMode(trigger_sol, OUTPUT);
pinMode(echo_sol, INPUT);

pinMode(trigger_sag, OUTPUT);
pinMode(echo_sag, INPUT);

// ön engel switchler pull-up olacak

pinMode(22,INPUT_PULLUP); // on_sol
pinMode(24,INPUT_PULLUP); //on_orta
pinMode(26,INPUT_PULLUP);

pinMode(51,INPUT);
pinMode(53,INPUT);

///*** Motorlar ve Switchler

for (byte pin = 0; pin < 4; pin++)
{
// Set the button switch pins for input
pinMode(SWITCH_PIN[pin], INPUT);

pinMode(m_PIN[pin], OUTPUT); digitalWrite(m_PIN[pin],LOW);
}

kafa_test();
ileri_git(); delay(1000);dur();
geri_git(); delay(200);dur();
sola_don(); delay(200);dur();
saga_don(); delay(200);dur();

// open serial communication for debugging
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(FILE);
Serial.print("Uploaded: "); Serial.println(DATE);
Serial.println(" ");

// open software serial connection to the Bluetooth module.
BTserial.begin(9600);
Serial.println("AltSoftSerial started at 9600");

newData = false;

}
//***********************************************************
//***********************************************************
//***********************************************************
//***********************************************************

void loop() {

hareket_sensor_kontrol();

engel_oku = digitalRead(22);
if (engel_oku == LOW) {sistem_test();delay(1000);}

engel_oku = digitalRead(24);
if (engel_oku == LOW) { sistem_test();delay(1000); }

engel_oku = digitalRead(26);

if (engel_oku == LOW) { sistem_test();delay(1000); }

for (byte switchNum = 1; switchNum < 5; switchNum++)
{
checkSwitch(switchNum);
}
recvWithStartEndMarkers(); // check to see if we have received any new commands
if (newData) { processCommand(); } // if we have a new command do something about it

}

//***********************************************************
//***********************************************************
//***********************************************************
//***********************************************************

void checkSwitch( byte pos)
{
// pos = 1,2,3. Array pos = 0,1,2 so convert by subtracting 1
pos = pos-1;

// very simple debouce.
boolean state1 = digitalRead(SWITCH_PIN[pos]); delay(1);
boolean state2 = digitalRead(SWITCH_PIN[pos]); delay(1);
boolean state3 = digitalRead(SWITCH_PIN[pos]); delay(1);
if ((state1 == state2) && (state1==state3))
{
switch_State[pos] = state1;
if ( (switch_State[pos] == HIGH) && (oldswitch_State[pos] == LOW) )
{
m_State[pos] = ! m_State[pos]; // flip the status.

char TMPcmd[8] = "<L,1,0>";
TMPcmd[3] = pos+1+48; // pos+1 should be 1,2,3 convert a numeric value to ascii by adding 48
TMPcmd[5] = m_State[pos]+48; // m_State should be 0 or 1
BTserial.print(TMPcmd);

digitalWrite(m_PIN[pos],m_State[pos]);
Serial.println(TMPcmd);
}
oldswitch_State[pos] = switch_State[pos];
}
}

void hareket_sensor_kontrol()
{
Serial.println(" Hareket Kontrol Ediliyor ");
int buttonState_sol = digitalRead(hareket_sensoru_sol);
int buttonState_sag = digitalRead(hareket_sensoru_sag);

if (buttonState_sol == HIGH || buttonState_sag == HIGH ) { Serial.println(" SOLDA Hareket ");sola_don(); delay(300);dur();saga_don(); delay(300);dur();sistem_test();delay(1000); }

}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++

void sola_don() // sola dön
{
digitalWrite(23, HIGH);digitalWrite(27, HIGH); delay(100);Serial.println(" SOLA donuyorum "); durum=1;
}

void saga_don()
{
digitalWrite(25, HIGH);digitalWrite(29, HIGH); delay(100); Serial.println(" SAGA donuyorum ");durum=1;
}

void ileri_git() // geri git
{
digitalWrite(23, HIGH);digitalWrite(29, HIGH); delay(100);Serial.println(" ilerliyorum ");durum=1;
}

void geri_git() // ileri olacak
{
digitalWrite(25, HIGH);digitalWrite(27, HIGH); delay(100);Serial.println(" GERi Gidiyorum ");durum=1;
}

void dur()
{
digitalWrite(23, LOW);
digitalWrite(25, LOW);
digitalWrite(27, LOW);
digitalWrite(29, LOW);
delay(200); Serial.println(" DURDUM ");
}
//*******************************************************//

void kafa_test()
{

for(i=1;i<=15;i=i+1)
{
aci=aci+5;
myservo.write(aci);
// sets the servo position according to the scaled value
delay(gecikme);
}
Serial.print("1= "); Serial.println(aci); delay(500);

for(i=1;i<=30;i=i+1)
{
aci=aci-5;
myservo.write(aci);
delay(gecikme);
}
Serial.print("2= "); Serial.println(aci); delay(500);
for(i=1;i<=15;i=i+1)
{
aci=aci+5;

myservo.write(aci);
// sets the servo position according to the scaled value
delay(gecikme);
}

Serial.print("3: "); Serial.println(aci); delay(500);
delay(2000);

}

void processCommand()
{
Serial.print("receivedChars = "); Serial.println(receivedChars);

if (receivedChars[0] == 'L')

{

byte mnum = receivedChars[1] - 48; // convert ascii to value by subtracting 48
boolean mstatus = receivedChars[2] - 48;

digitalWrite(m_PIN[mnum-1],mstatus);
m_State[mnum-1] = mstatus;
}

receivedChars[0] = '\0';
newData = false;
}

void sistem_test()
{

for(i=1;i<=5;i=i+1)
{
aci=aci+5;
myservo.write(aci);
// sets the servo position according to the scaled value
delay(gecikme);
}
Serial.print("test1= "); Serial.println(aci); delay(500);

for(i=1;i<=5;i=i+1)
{
aci=aci-5;
myservo.write(aci);
delay(gecikme);
}
Serial.print("test2= "); Serial.println(aci); delay(500);

}

void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;

if (BTserial.available() > 0)
{
rc = BTserial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx > maxDataLength) { ndx = maxDataLength; }
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) { recvInProgress = true; }
}
}

sketch_apr30a.ino (7.69 KB)

Hi there!

I see you have included two libraries, the Servo library and AltSoftSerial. Most likely these two libraries are trying to use the same timer on the microcontroller at the same time, which would give you this error. Trying using the ServoTimer2 library and everything should be fine.

Good luck!

bos1714:
Hi there!

I see you have included two libraries, the Servo library and AltSoftSerial. Most likely these two libraries are trying to use the same timer on the microcontroller at the same time, which would give you this error. Trying using the ServoTimer2 library and everything should be fine.

Good luck!

Thanks, i compiled and it's done. I changed this;

#include <Servo.h>
int aci=90,i;
Servo myservo;
int gecikme=40;

to that;

#include <ServoTimer2.h>
int aci=90,i;
ServoTimer2 myservo;
int gecikme=40;

By the way, i included the ServoTimer2 Zip Library on my Arduino IDE. Propably there is not anything else I need to change. Thank you sir, you are a hero.

Congrats :slight_smile: :slight_smile: