Problem combining 2 separate codes

Hello

I need help with my project. It is basically a robot that needs to follow a line, but a the end of the line (when no sensors see a line) the robot has to start a other sequence, which is finding an object with an ultrasonic sensor. (right now it can only measure the distance)

I have 2 seperate codes that both work separately with my robot. But when I want to merge them together I get error's.
I would really appreciate any help or interest!

I wish to combine 2 codes using following code;

bool lijnVolgen = true;

void setup() {
  // SETUP1
  // SETUP2

}

void loop() {
  if(lijnVolgen == true){
    //PROGRAM1
    
    if(out1==1  && out2==1 && out3==1 && out4==1 && out5==1){ // When none of the 5 IR-sensors see a line
      lijnVolgen = false;
    }
  }else if(lijnVolgen == false){
    // PROGRAM 2
    
  }
}

Here are the 2 codes I want to combine;
CODE1: line following

#define RECHTS_DIR 13 
#define RECHTS_SPEED 11
#define RECHTS_BRAKE 8

#define LINKS_DIR 12
#define LINKS_SPEED 3
#define LINKS_BRAKE 9

#define SENSOR_1 2
#define SENSOR_2 4
#define SENSOR_3 5
#define SENSOR_4 10
#define SENSOR_5 A2

#define VOORUIT HIGH
#define ACHTERUIT LOW

int out1=0;
int out2=0;
int out3=0;
int out4=0;
int out5=0;

void setup() {

  pinMode(LINKS_DIR, OUTPUT);
  pinMode(LINKS_SPEED, OUTPUT);
  pinMode(LINKS_BRAKE, OUTPUT);
  pinMode(RECHTS_DIR, OUTPUT);
  pinMode(RECHTS_SPEED, OUTPUT);
  pinMode(RECHTS_BRAKE, OUTPUT);

  pinMode(SENSOR_1, INPUT);
  pinMode(SENSOR_2, INPUT);
  pinMode(SENSOR_3, INPUT);
  pinMode(SENSOR_4, INPUT);
  pinMode(SENSOR_5, INPUT);


  digitalWrite(LINKS_BRAKE, LOW);
  digitalWrite(RECHTS_BRAKE, LOW);
}

void loop()

{
  out1 = digitalRead(SENSOR_1);
  out2 = digitalRead(SENSOR_2);
  out3 = digitalRead(SENSOR_3);
  out4 = digitalRead(SENSOR_4);
  out5 = digitalRead(SENSOR_5);

  if(out3==0 && out1==1 && out2==1 && out4==1 && out5==1) // Als middensensor alleen lijn ziet (voorwaarde andere sensoren zien geen lijn toegevoegd)
  
  {
    analogWrite(LINKS_SPEED,255);  //beide motors maximum
    analogWrite(RECHTS_SPEED,255);  
 digitalWrite(LINKS_DIR, VOORUIT);
  digitalWrite(RECHTS_DIR, VOORUIT);    
  } 
  else if(out4==0 && out3==1) // als rechts4 lijn ziet maar midden niet (kleine bocht)
  {
    if(out3 != 0) 
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
        digitalWrite(LINKS_DIR, VOORUIT);
  digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }
  else if(out4==0 && out3==0) // als middensensor lijn ziet en rechts4 (erg kleine bocht)
  {
    if(out4 != 1)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
        digitalWrite(LINKS_DIR, VOORUIT);
  digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }

  else if(out5 ==0 && out3==1) // als rechts5 lijn ziet en midden niet (scherpe bocht)
  {
    if(out3 != 0)
    {
      analogWrite(RECHTS_SPEED,220);
      analogWrite(LINKS_SPEED,220);
        digitalWrite(LINKS_DIR, VOORUIT);
  digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }
  else if(out5==0 && out3==0) // als rechts5 lijn ziet en midden lijn ziet (ERG SCHERP)
  {
    if(out5 != 1)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
        digitalWrite(LINKS_DIR, VOORUIT);
  digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }
    else if(out5==0 && out3==0) // als rechts5 lijn ziet en midden lijn ziet (ERG SCHERP)
  {
    if(out5 != 1)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
        digitalWrite(LINKS_DIR, VOORUIT);
  digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }

  else if(out2==0 && out3==1) // als links2 lijn ziet maar midden niet
  {
    if(out3 != 0)
    {
      digitalWrite(LINKS_SPEED,220);
      analogWrite(RECHTS_SPEED,220);
        digitalWrite(LINKS_DIR, ACHTERUIT);
  digitalWrite(RECHTS_DIR, VOORUIT);
    }
  }
  else if(out2==0 && out3==0) // als links2 lijn ziet en midden ook
  {
    if(out2 != 1)
    {
      analogWrite(LINKS_SPEED,255);
      analogWrite(RECHTS_SPEED,255);
       digitalWrite(LINKS_DIR, ACHTERUIT);
  digitalWrite(RECHTS_DIR, VOORUIT);
    }
  }
  else if( out2==0 && out1==0) // als links2 lijn ziet en links1 ook (redelijk scherpe bocht)
  {
    if(out3 !=0)
    {
      analogWrite(RECHTS_SPEED,220);
      analogWrite(LINKS_SPEED,220);
       digitalWrite(LINKS_DIR, ACHTERUIT);
  digitalWrite(RECHTS_DIR, VOORUIT);
    }

  }
  else if(out1==0) // als links1 alleen lijn ziet (erg scherpe bocht)
  {
    if(out3 !=0)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
       digitalWrite(LINKS_DIR, ACHTERUIT);
  digitalWrite(RECHTS_DIR, VOORUIT);
    }


  }


  else if(out1==1 && out2==1 && out3==1 && out4==1 && out5==1) // als alle sensors geen lijn zien
  {
    digitalWrite(RECHTS_SPEED,LOW);
    digitalWrite(LINKS_SPEED,LOW);
  }
  delay(30);
}

CODE2: distance measuring

#include "URMSerial.h"
#include <SoftwareSerial.h>

// The measurement we're taking
#define DISTANCE 1
#define TEMPERATURE 2
#define ERROR 3
#define NOTREADY 4
#define TIMEOUT 5

URMSerial urm;

void setup() {

  Serial.begin(9600);                  // Sets the baud rate to 9600
  urm.begin(7,6,9600);                 // RX Pin, TX Pin, Baud Rate
  Serial.println("URM37 Library by Miles Burton");   // Shameless plug 
  
}

void loop()
{
  Serial.print("Measurement: ");
  Serial.println(getMeasurement());  // Output measurement
  delay(50);
}


int value; // This value will be populated
int getMeasurement()
{
  // Request a distance reading from the URM37
  switch(urm.requestMeasurementOrTimeout(DISTANCE, value)) // Find out the type of request
  {
  case DISTANCE: // Double check the reading we recieve is of DISTANCE type
    //    Serial.println(value); // Fetch the distance in centimeters from the URM37
    return value;
    break;
  case TEMPERATURE:
    return value;
    break;
  case ERROR:
    Serial.println("Error");
    break;
  case NOTREADY:
    Serial.println("Not Ready");
    break;
  case TIMEOUT:
    Serial.println("Timeout");
    break;
  } 

  return -1;
}

They both work separately but when I combine them I get an error's.

Can someone please give me advice on how to do this?

They both work separately but when I combine them I get an error's.

Can someone please give me advice on how to do this?

Sure. Fix the errors.

If you don't know how, you need to at least tell us what they are.

Yes, I'm sorry.

The error is;

'getMeasurement' was not declared in this scope

in

Serial.println(getMeasurement());

though I don't get this error when I run the program separately

The first code you posted - you're attempt to combine them - does not contain that statement.

Enough of the guessing games. Post the code that won't compile.

Or read this:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Grumpy_Mike:
Or read this:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Or? That should be And, shouldn't it?

Function must be defined before its call. You missed prototype of this function or move its definition in source before a call.

Function must be defined before its call

While this is quite true in normal C/C++, it's not in Arduino world; the IDE takes care of creating prototypes for you behind the scenes.

wildbill:

Function must be defined before its call

While this is quite true in normal C/C++, it's not in Arduino world; the IDE takes care of creating prototypes for you behind the scenes.

No. The IDE takes care of defining the function prototype. You still must define the function before you can call it.

Grumpy_Mike:
Or read this:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

I tried this but it's really not working...

Here is my "merged" codes, and my errors.
I am very new to Arduino, I thank everyone for their efforts to help me.

#include <SoftwareSerial.h>

#include "URMSerial.h"



#define RECHTS_DIR 13  // MOTOR
#define RECHTS_SPEED 11
#define RECHTS_BRAKE 8

#define LINKS_DIR 12
#define LINKS_SPEED 3
#define LINKS_BRAKE 9

#define SENSOR_1 2 // IR
#define SENSOR_2 4
#define SENSOR_3 5
#define SENSOR_4 10
#define SENSOR_5 A2

#define DISTANCE 1 // URM
#define TEMPERATURE 2
#define ERROR 3
#define NOTREADY 4



#define VOORUIT HIGH
#define ACHTERUIT LOW

int out1=0;
int out2=0;
int out3=0;
int out4=0;
int out5=0;

bool lijnVolgen = true;




void setup() {
  
  Serial.begin(9600);                  // URM

  pinMode(LINKS_DIR, OUTPUT);          // MOTOR
  pinMode(LINKS_SPEED, OUTPUT);
  pinMode(LINKS_BRAKE, OUTPUT);
  pinMode(RECHTS_DIR, OUTPUT);
  pinMode(RECHTS_SPEED, OUTPUT);
  pinMode(RECHTS_BRAKE, OUTPUT);

  pinMode(SENSOR_1, INPUT);          // IR
  pinMode(SENSOR_2, INPUT);
  pinMode(SENSOR_3, INPUT);
  pinMode(SENSOR_4, INPUT);
  pinMode(SENSOR_5, INPUT);


  digitalWrite(LINKS_BRAKE, LOW);      // MOTOR rem
  digitalWrite(RECHTS_BRAKE, LOW);
}




void loop() {
  // put your main code here, to run repeatedly:
  if(lijnVolgen == true){
  
 {
  out1 = digitalRead(SENSOR_1);
  out2 = digitalRead(SENSOR_2);
  out3 = digitalRead(SENSOR_3);
  out4 = digitalRead(SENSOR_4);
  out5 = digitalRead(SENSOR_5);

  if(out3==0 && out1==1 && out2==1 && out4==1 && out5==1)
  // Als middensensor alleen lijn ziet (voorwaarde andere sensoren zien geen lijn toegevoegd)

  {
    analogWrite(LINKS_SPEED,255);  //beide motors maximum
    analogWrite(RECHTS_SPEED,255);  
    digitalWrite(LINKS_DIR, VOORUIT);
    digitalWrite(RECHTS_DIR, VOORUIT);    
  } 
  else if(out4==0 && out3==1) // als rechts4 lijn ziet maar midden niet (kleine bocht)
  {
    if(out3 != 0) 
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
      digitalWrite(LINKS_DIR, VOORUIT);
      digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }
  else if(out4==0 && out3==0) // als middensensor lijn ziet en rechts4 (erg kleine bocht)
  {
    if(out4 != 1)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
      digitalWrite(LINKS_DIR, VOORUIT);
      digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }

  else if(out5 ==0 && out3==1) // als rechts5 lijn ziet en midden niet (scherpe bocht)
  {
    if(out3 != 0)
    {
      analogWrite(RECHTS_SPEED,220);
      analogWrite(LINKS_SPEED,220);
      digitalWrite(LINKS_DIR, VOORUIT);
      digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }
  else if(out5==0 && out3==0) // als rechts5 lijn ziet en midden lijn ziet (ERG SCHERP)
  {
    if(out5 != 1)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,200);
      digitalWrite(LINKS_DIR, VOORUIT);
      digitalWrite(RECHTS_DIR, ACHTERUIT);
    }
  }

  else if(out2==0 && out3==1) // als links2 lijn ziet maar midden niet
  {
    if(out3 != 0)
    {
      digitalWrite(LINKS_SPEED,220);
      analogWrite(RECHTS_SPEED,220);
      digitalWrite(LINKS_DIR, ACHTERUIT);
      digitalWrite(RECHTS_DIR, VOORUIT);
    }
  }
  else if(out2==0 && out3==0) // als links2 lijn ziet en midden ook
  {
    if(out2 != 1)
    {
      analogWrite(LINKS_SPEED,255);
      analogWrite(RECHTS_SPEED,255);
      digitalWrite(LINKS_DIR, ACHTERUIT);
      digitalWrite(RECHTS_DIR, VOORUIT);
    }
  }
  else if( out2==0 && out1==0) // als links2 lijn ziet en links1 ook (redelijk scherpe bocht)
  {
    if(out3 !=0)
    {
      analogWrite(RECHTS_SPEED,220);
      analogWrite(LINKS_SPEED,220);
      digitalWrite(LINKS_DIR, ACHTERUIT);
      digitalWrite(RECHTS_DIR, VOORUIT);
    }

  }
  else if(out1==0) // als links1 alleen lijn ziet (erg scherpe bocht)
  {
    if(out3 !=0)
    {
      analogWrite(RECHTS_SPEED,255);
      analogWrite(LINKS_SPEED,255);
      digitalWrite(LINKS_DIR, ACHTERUIT);
      digitalWrite(RECHTS_DIR, VOORUIT);
    }


  }



  delay(30);
}
   
    
    
    
    
    if(out1 == HIGH && out2 == HIGH && out3== HIGH && out4==HIGH && out5==HIGH){
      //geen lijn
      lijnVolgen = false;
    }
  }else if(lijnVolgen == false){
  
Serial.print("Measurement: ");
  Serial.println(getMeasurement());  // Output measurement
  delay(50);
}


int value; // This value will be populated
int getMeasurement()
{
  // Request a distance reading from the URM37
  switch(urm.requestMeasurementOrTimeout(DISTANCE, value)) // Find out the type of request
  {
  case DISTANCE: // Double check the reading we recieve is of DISTANCE type
    //    Serial.println(value); // Fetch the distance in centimeters from the URM37
    return value;
    break;
  case TEMPERATURE:
    return value;
    break;
  case ERROR:
    Serial.println("Error");
    break;
  case NOTREADY:
    Serial.println("Not Ready");
    break;
  case TIMEOUT:
    Serial.println("Timeout");
    break;
  } 

  return -1;
}
}

Errors;

ArduinoRobotURM_LIJN.ino: In function 'void loop()':
ArduinoRobotURM_LIJN:189: error: 'getMeasurement' was not declared in this scope
ArduinoRobotURM_LIJN:196: error: a function-definition is not allowed here before '{' token
ArduinoRobotURM_LIJN:220: error: expected `}' at end of input

Your indenting style needs some work. Start at the top of the code, and manually indent everything properly. When you can't, you'll see what the problem is.

...and also you will need function prototype

int getMeasurement(); // prototype

insert this above setup() function body. :roll_eyes:
Self-consistency!

Budvar10:
...and also you will need function prototype

int getMeasurement(); // prototype

insert this above setup() function body. :roll_eyes:
Self-consistency!

The IDE creates function prototypes. This is not required. Though it does no harm.

To PaulS: Yes, that is right, in simple code compiler doesn't complain... :grin:
...however, I recommend to follow this rule!

Budvar10:
in simple code compiler doesn't complain...

So you're saying the compiler will complain if the code is complex?

I think yes, but I never tracked this thing in details. In simple cases it is need not #include directives to have included all stuff that is true e.g. However, for the future, for larger projects, and if you are not use IDE it will be needed, I am sure. This is standard C language requirement.

I think yes,

You think wrong.

and if you are not use IDE it will be needed

Sure but in case you haven't noticed this is an Arduino forum.

SiemenLec:

Grumpy_Mike:
Or read this:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

I tried this but it's really not working...

That is because you didn't do it correctly as suggested by the web page.
You tried to stuff all the code into one loop function and you screwed that up by not getting your braces matching.

If you click to the right of an opening brace the matching closing brace will be highlighted. If you do that for your loop function the brace at the very end of the code is highlighted which means you have not got each function definition in braces.

Try again.