Problem in running an 'if' 'else' statement with a mini project

Hello I am trying program my arduino MEGA 2560 for sensing a distance of an object in cms and maintain pressure inside a chamber according to the distance sensed. So I used 1) Ultrasonic distance sensor 2) pressure MAP sensor which gives analog values based on the pressure readings 3) A servo to release the pressure inside the chamber using a valve 4) relay to increase the pressure inside the chamber. I want the device to work whenever it senses an object at a particular distance it needs to decide what pressure it should be maintaining inside the chamber (Say if the pressure is more the release valve will open or if the pressure is less the relay should work) So I took the readings by observing the MAP sensor readings and the distance sensor readings which results in a linear graph with a relation (Y=199.81*D-2757). I tried to multiplex all these programs into a single program but have got errors in the function bit. Please help me to solve this.. I am attaching the source code with this message as a wordpad file. If you edit it please highlight it so that it can be easy for me to understand

prog doubt.rtf (2.69 KB)

This code on lines 57-88 is not inside a function:

if(Y==V)
{
  digitalWrite(Relay_1,RELAY_OFF);
  //close
   for(pos=118;pos>=6;pos-=4)
  {
    myservo.write(pos);
    delay(15);
  }
} 
else if(Y<V)
{

 digitalWrite(Relay_1,RELAY_ON);
//close 
 for(pos=118;pos>=6;pos-=4)
  {
    myservo.write(pos);
    delay(15);
  }

}
else 
{
 digitalWrite(Relay_1,RELAY_OFF);
 //open
  for(pos=6;pos<118;pos+=4)
  {
    myservo.write(pos);
    delay(15);
  }
}

Perhaps you meant to include this code fragment at the end of the loop() function?

I get error in this line

D = microsecondsToCentimeters(duration);

Can you please help with my case ?

You need to remove the semicolon at the end of line 49 to turn that into a valid function definition to resolve the error at line 37.

I suggest that you sort out that code which is not inside a function next since it will never compile like that.

For future reference, if you want help resolving a compilation error you need to post the code that causes the error and the complete error message.

It says 'microsecondToCentimeters' was not declared in this scope

sketch_aug02a.ino: In function 'void loop()':
sketch_aug02a:39: error: 'microsecondsToCentimeters' was not declared in this scope
sketch_aug02a:83: error: a function-definition is not allowed here before '{' token

Someone already told you what part of your problem was. The second problem was adding a semicolon at the end of your method definition

long microsecondsToCentimeters(long microseconds); <-Right Here, remove this
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Pending any other errors, it should compile fine.