user43465: I try to use this code, but it shows that I need to define again the last void turn_mer (int direction) function. How can I fix this?
#include <Servo.h> //create servo object to control a servo.
Servo myservo;

//pin configuration.
const byte servoPin = 2; const byte motorLeft = 5; const byte motorRight = 6;
const byte echoPin = 12; const byte trigPin = 13; const byte echoPin2 = 8; const byte trigPin2 = 9; // LET OP DE HOOFDLETTERS: echopin en echoPin
//delay determines how long the MER turns, increase to make the turning angle larger.
const int turnDelay = 500;
//standard angle for looking straight ahead, compensate for construction errors.
const int standardAngle = 90;
//angle in degrees that controls the direction in which the sensor looks.
const int viewAngle = 40;
//delay to let servo turn, not optimized; for smaller angles smaller delay could be used.
const int servoDelay = 250;
//object distance in cm at which the MER decides to look around and turn another way.
const int lineOfSight = 40;
//pwm duty cycle of the motor, use a value from 0 (no movement) to 255 (full speed).
const int motorSpeed = 90;

//const int lineDownSight = 35;
void setup() //setup pin configuration and initialize the sensor position.


{ delay(3000); //delay to give us some time to place the MER on the ground.
  myservo.attach(servoPin); //initialize the servo and look straight ahead.
  turn_servo(standardAngle);
  pinMode(motorLeft, OUTPUT); pinMode(motorRight, OUTPUT);
  pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); drive_mer();
} //start the mer.
void drive_mer() //this function lets the MER drive forward.
{
  analogWrite(motorLeft, motorSpeed);
  analogWrite(motorRight, motorSpeed);
}
void stop_mer() //this function stops the mer.
{
  digitalWrite(motorLeft, LOW);
  digitalWrite(motorRight, LOW);
}
void turn_servo(int angle)
//turn servo to given angle, do not return until angle is reached.
//if the servo is already at the correct angle, we don't have to do anything.
{ if (angle == myservo.read()) {
    return;
  }
  else {
    myservo.write(angle);
    delay(servoDelay);
  }
}



int look1() //looking function, returns distance in the observed direction as an integer.
{ int duration, distance;
  digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1; return distance;
}
//turning function: makes MER turn in preferred direction. 0 for left and 1 for right.
void turn_mer(int direction)


{ if (direction == 0) {
    digitalWrite(motorLeft, LOW);
    digitalWrite(motorRight, HIGH);
  }
  else {
    digitalWrite(motorLeft, HIGH);
    digitalWrite(motorRight, LOW);
  }
  delay(turnDelay); stop_mer();
}//stop the engines when the MER has turned far enough.

//this function turns the MER depending on the observed distances.
void handle_measurements(int distanceLeft, int distanceStraight, int distanceRight)// int distanceDown

{ if (distanceLeft < lineOfSight and distanceStraight < lineOfSight and
      distanceRight < lineOfSight) {
    stop_mer();
  }

  else if (distanceLeft < lineOfSight) {
    turn_mer(1);
    drive_mer();
  }

  else if (distanceStraight < lineOfSight) {
    if (distanceRight > distanceLeft)
    {
      turn_mer(1);
    } else {
      turn_mer(0);
    } drive_mer();
  }

  else if (distanceRight < lineOfSight) {
    turn_mer(0);
    drive_mer();
  }
 //if (distanceDown > lineDownSight) {
  //  stop_mer();
  }

  void loop()
  { int distanceLeft, distanceStraight, distanceRight;

    turn_servo(standardAngle + viewAngle); distanceLeft = look1();
    turn_servo(standardAngle); distanceStraight = look1();
    turn_servo(standardAngle - viewAngle); distanceRight = look1();
       handle_measurements(distanceLeft, distanceStraight, distanceRight);}

/*  void handle_measurements(lineDownSight)
   {(distanceDown > lineDownSight) {
    stop_mer();
  }*/
  //                                                                                   sensor 2
  //______________________________________________________________________________________________________________________________________________________________________________________________________

 const int lineDownSight = 35;

  void handle_measurements (int distanceDown)

{if (distanceDown > lineDownSight) {
    stop_mer();}}


  int look2() //looking function, returns distance in the observed direction as an integer.
  { int duration, distance;
    digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH);
    distance = (duration / 2) / 29.1; return distance;
  }
  //turning function: makes MER turn in preferred direction. 0 for left and 1 for right.
void turn_mer (int direction)

{handle_measurements(distanceDown)}


user31105: But the main problem is that in C, you can only define a function with the same name once.
Halfway you defined the function turn_mer already, and again at the end.
Also, you are missing an end colon in:
{handle_measurements(distanceDown)}

It's better to align your code like:
{
    handle_measurements(distanceDown);
}


43465: <p>I try to use this code, but it shows that I need to define again the last void turn_mer (int direction) function. How can I fix this?</p>

<pre><code>#include &lt;Servo.h&gt; //create servo object to control a servo.
Servo myservo;

//pin configuration.
const byte servoPin = 2; const byte motorLeft = 5; const byte motorRight = 6;
const byte echoPin = 12; const byte trigPin = 13; const byte echoPin2 = 8; const byte trigPin2 = 9; // LET OP DE HOOFDLETTERS: echopin en echoPin
//delay determines how long the MER turns, increase to make the turning angle larger.
const int turnDelay = 500;
//standard angle for looking straight ahead, compensate for construction errors.
const int standardAngle = 90;
//angle in degrees that controls the direction in which the sensor looks.
const int viewAngle = 40;
//delay to let servo turn, not optimized; for smaller angles smaller delay could be used.
const int servoDelay = 250;
//object distance in cm at which the MER decides to look around and turn another way.
const int lineOfSight = 40;
//pwm duty cycle of the motor, use a value from 0 (no movement) to 255 (full speed).
const int motorSpeed = 90;

//const int lineDownSight = 35;
void setup() //setup pin configuration and initialize the sensor position.


{ delay(3000); //delay to give us some time to place the MER on the ground.
  myservo.attach(servoPin); //initialize the servo and look straight ahead.
  turn_servo(standardAngle);
  pinMode(motorLeft, OUTPUT); pinMode(motorRight, OUTPUT);
  pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); drive_mer();
} //start the mer.
void drive_mer() //this function lets the MER drive forward.
{
  analogWrite(motorLeft, motorSpeed);
  analogWrite(motorRight, motorSpeed);
}
void stop_mer() //this function stops the mer.
{
  digitalWrite(motorLeft, LOW);
  digitalWrite(motorRight, LOW);
}
void turn_servo(int angle)
//turn servo to given angle, do not return until angle is reached.
//if the servo is already at the correct angle, we don't have to do anything.
{ if (angle == myservo.read()) {
    return;
  }
  else {
    myservo.write(angle);
    delay(servoDelay);
  }
}



int look1() //looking function, returns distance in the observed direction as an integer.
{ int duration, distance;
  digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1; return distance;
}
//turning function: makes MER turn in preferred direction. 0 for left and 1 for right.
void turn_mer(int direction)


{ if (direction == 0) {
    digitalWrite(motorLeft, LOW);
    digitalWrite(motorRight, HIGH);
  }
  else {
    digitalWrite(motorLeft, HIGH);
    digitalWrite(motorRight, LOW);
  }
  delay(turnDelay); stop_mer();
}//stop the engines when the MER has turned far enough.

//this function turns the MER depending on the observed distances.
void handle_measurements(int distanceLeft, int distanceStraight, int distanceRight)// int distanceDown

{ if (distanceLeft &lt; lineOfSight and distanceStraight &lt; lineOfSight and
      distanceRight &lt; lineOfSight) {
    stop_mer();
  }

  else if (distanceLeft &lt; lineOfSight) {
    turn_mer(1);
    drive_mer();
  }

  else if (distanceStraight &lt; lineOfSight) {
    if (distanceRight &gt; distanceLeft)
    {
      turn_mer(1);
    } else {
      turn_mer(0);
    } drive_mer();
  }

  else if (distanceRight &lt; lineOfSight) {
    turn_mer(0);
    drive_mer();
  }
 //if (distanceDown &gt; lineDownSight) {
  //  stop_mer();
  }

  void loop()
  { int distanceLeft, distanceStraight, distanceRight;

    turn_servo(standardAngle + viewAngle); distanceLeft = look1();
    turn_servo(standardAngle); distanceStraight = look1();
    turn_servo(standardAngle - viewAngle); distanceRight = look1();
       handle_measurements(distanceLeft, distanceStraight, distanceRight);}

/*  void handle_measurements(lineDownSight)
   {(distanceDown &gt; lineDownSight) {
    stop_mer();
  }*/
  //                                                                                   sensor 2
  //______________________________________________________________________________________________________________________________________________________________________________________________________

 const int lineDownSight = 35;

  void handle_measurements (int distanceDown)

{if (distanceDown &gt; lineDownSight) {
    stop_mer();}}


  int look2() //looking function, returns distance in the observed direction as an integer.
  { int duration, distance;
    digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH);
    distance = (duration / 2) / 29.1; return distance;
  }
  //turning function: makes MER turn in preferred direction. 0 for left and 1 for right.
void turn_mer (int direction)

{handle_measurements(distanceDown)}
</code></pre>
 31105: <p>But the main problem is that in C, you can only define a function with the same name once.</p>

<p>Halfway you defined the function turn_mer already, and again at the end.</p>

<p>Also, you are missing an end colon in:</p>

<pre><code>{handle_measurements(distanceDown)}
</code></pre>

<p>It's better to align your code like:</p>

<pre><code>{
    handle_measurements(distanceDown);
}
</code></pre>
