Can't Identify the errors in the code

I am trying to make an obstacle avoiding robot with two IR sensors and an ultrasonic distance sensor. I put in the code for the HC-SR04 just yesterday and my code is not working. I put in the code for the IR sensors and it worked perfectly. I can’t identify what is wrong with the code ever since I put the code for the HC-SR04 into the code I have for the IR sensors. I have to tweak the pin numbers since I noticed that I repeated the pin numbers when declaring my variables. I also separated each section so it is easier to find where I declared my variables, the setup, and the loop. Any suggestions that could help me with these error messages? Thanks.

/*keep a bot close to a wall without touching it but also avoid obstacles
  *Rideck Deckard, Cody Hubert 
  *Rochester High School
  * 12/1/2016
  */
 //------------------------------------------------------DECLARE VARIABLES-----------------------------------------------------
 const int pingPin = 7;
 int TrigPin = 11; //a pin
 int EchoPin = 12;//a pin
 int enA = 10;//a pin
 int in1 = 9;//a pin
 int in2 = 8;//a pin
 int enB = 5;//a pin
 int in3 = 7;//a pin
 int in4 = 6;//a pin
 int enC = 10;//a pin
 int in5 = 9;//a pin
 int in6 = 8;//a pin
 int enD = 5;//a pin
 int in7 = 7;//a pin
 int in8 = 6;//a pin
 int distance;//the distance
 long duration;//distance traveled for x amount of time before stopping
 int green = 4;//the green LED
 int red = 3;//the red LED
 //-------------------------------------------------------MAIN SETUP----------------------------------------------------------
 void setup(){
  pinMode(enA,OUTPUT);//enA is now a pin
  pinMode(enB,OUTPUT);//enB is now a pin
  pinMode(in1,OUTPUT);//in1 is now a pin
  pinMode(in2,OUTPUT);//in2 is now a pin
  pinMode(in3,OUTPUT);//in3 is now a pin
  pinMode(in4,OUTPUT);//in4 is now a pin
  pinMode(TrigPin,OUTPUT);//a pin
  pinMode(EchoPin,INPUT);//a pin
  pinMode(green,OUTPUT);//a pin
  pinMode(red,OUTPUT);//a pin
  Serial.begin(9600);//opens serial port,sets data rate to 9600 bps
 }
 //-------------------------------------------------------MAIN PROGRAM--------------------------------------------------------
 void loop(){
   long duration, inches, cm;//duration of microseconds converted into distance
  pinMode(pingPin, OUTPUT);//set pingPin as an output
  digitalWrite(pingPin, LOW);//turn pingPin off
  delayMicroseconds(2);//delay for 2 microseconds
  digitalWrite(pingPin, HIGH);//turn on pingPin
  delayMicroseconds(5);//delay for 5 microseconds
  digitalWrite(pingPin, LOW);//turn pingPin off
  pinMode(pingPin, INPUT);//set pingPin as an input
  duration = pulseIn(pingPin, HIGH);// convert the time into a distance
  inches = microsecondsToInches(duration);//in is how many microseconds it takes for the PING))) to come back converted from time
  cm = microsecondsToCentimeters(duration);//cm is how many microseconds it takes for the PING))) to come back converted from time
  Serial.print(inches);//opens serial port, turns time into in
  Serial.print("in, ");//opens serial port, turns time into estimated in
  Serial.print(cm);//opens serial port, turns time into cm
  Serial.print("cm");//opens serial port, converts time into estimated cm
  delay(100);//delay for 1 second
}

long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;//return the amount of microseconds as inches 
}

long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;//return the amount of microseconds as centimeters
}
  digitalWrite(TrigPin,LOW);//turn TrigPin off
  delayMicroseconds(2000);//delay for 2 seconds
  digitalWrite(TrigPin,HIGH);//turn TrigPin on
  delayMicroseconds(2000);//delay for 2 seconds
  digitalWrite(TrigPin,LOW);//turn TrigPin off
  duration = pulseIn(EchoPin,HIGH);//use EchoPin to calculate the duration
  distance = (duration/2)/29.1;//equation to turn the duration into distance
  Serial.println(distance);//opens serial port and sets data rate to the distance 
  digitalWrite(green,LOW);//green is off
  digitalWrite(red,LOW);//red is off
  if (distance >= 10 ){
    turnRight();//If distance is greater than 20cm, go right
    digitalWrite(green,HIGH);//green is on
  }
  else if (distance < 10){
    turnLeft();//If distance is less than 10cm, go left
    digitalWrite(red,HIGH);//red is on
    
  }
  else if ((distance==0)||(distance>100)){
    digitalWrite(in1,LOW);//turn off
    digitalWrite(in2,LOW);//turn off
    digitalWrite(in3,LOW);//turn off
    digitalWrite(in4,LOW);//turn off
    digitalWrite(in5,LOW);//turn off
    digitalWrite(in6,LOW);//turn off
    digitalWrite(in7,LOW);//turn off
    digitalWrite(in8,LOW);//turn off
  }
 void turnRight(){
  digitalWrite(in1,HIGH);//in1 power
  digitalWrite(in2,LOW);// in2 ground
  analogWrite(enA,240);// speed of motor A
  digitalWrite(in3,HIGH);// in3 power
  digitalWrite(in4,LOW);//in4 ground
  analogWrite(enB,150);//speed for motor B
  digitalWrite(in5,HIGH);// in5 power
  digitalWrite(in6,LOW);//in6 ground
  analogWrite(enC,240);//speed for motor C
  digitalWrite(in7,HIGH);// in7 power
  digitalWrite(in8,LOW);//in8 ground
  analogWrite(enD,150);//speed for motor D
 }
 void turnLeft(){
  digitalWrite(in1,HIGH);//in1 power
  digitalWrite(in2,LOW);// in2 ground
  analogWrite(enA,245);//speed of motor A
  digitalWrite(in3,HIGH);//in3 power
  digitalWrite(in4,LOW);//in4 ground
  analogWrite(enB,255);//speed for motor B
  digitalWrite(in3,HIGH);//in5 power
  digitalWrite(in4,LOW);//in6 ground
  analogWrite(enC,245);//speed for motor C
  digitalWrite(in7,HIGH);//in7 power
  digitalWrite(in8,LOW);//in8 ground
  analogWrite(enD,255);//speed for motor D
 }

Here are my errors:

sketch_dec17a:69: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(TrigPin,LOW);//turn TrigPin off

               ^

sketch_dec17a:70: error: expected constructor, destructor, or type conversion before '(' token

   delayMicroseconds(2000);//delay for 2 seconds

                    ^

sketch_dec17a:71: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(TrigPin,HIGH);//turn TrigPin on

               ^

sketch_dec17a:72: error: expected constructor, destructor, or type conversion before '(' token

   delayMicroseconds(2000);//delay for 2 seconds

                    ^

sketch_dec17a:73: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(TrigPin,LOW);//turn TrigPin off

               ^

sketch_dec17a:74: error: 'duration' does not name a type

   duration = pulseIn(EchoPin,HIGH);//use EchoPin to calculate the duration

   ^

sketch_dec17a:75: error: 'distance' does not name a type

   distance = (duration/2)/29.1;//equation to turn the duration into distance

   ^

sketch_dec17a:76: error: 'Serial' does not name a type

   Serial.println(distance);//opens serial port and sets data rate to the distance 

   ^

sketch_dec17a:77: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(green,LOW);//green is off

               ^

sketch_dec17a:78: error: expected constructor, destructor, or type conversion before '(' token

   digitalWrite(red,LOW);//red is off

               ^

sketch_dec17a:79: error: expected unqualified-id before 'if'

   if (distance >= 10 ){

   ^

sketch_dec17a:83: error: expected unqualified-id before 'else'

   else if (distance < 10){

   ^

sketch_dec17a:88: error: expected unqualified-id before 'else'

   else if ((distance==0)||(distance>100)){

   ^

exit status 1
expected constructor, destructor, or type conversion before '(' token

The line that the compiler complains about and the lines that follow don't seem to be inside a function.

The following code is outside any function.

Place it properly within whatever function you meant it to be in.

  digitalWrite(TrigPin,LOW);//turn TrigPin off
  delayMicroseconds(2000);//delay for 2 seconds
  digitalWrite(TrigPin,HIGH);//turn TrigPin on
  delayMicroseconds(2000);//delay for 2 seconds
  digitalWrite(TrigPin,LOW);//turn TrigPin off
  duration = pulseIn(EchoPin,HIGH);//use EchoPin to calculate the duration
  distance = (duration/2)/29.1;//equation to turn the duration into distance
  Serial.println(distance);//opens serial port and sets data rate to the distance 
  digitalWrite(green,LOW);//green is off
  digitalWrite(red,LOW);//red is off
  if (distance >= 10 ){
    turnRight();//If distance is greater than 20cm, go right
    digitalWrite(green,HIGH);//green is on
  }
  else if (distance < 10){
    turnLeft();//If distance is less than 10cm, go left
    digitalWrite(red,HIGH);//red is on
    
  }
  else if ((distance==0)||(distance>100)){
    digitalWrite(in1,LOW);//turn off
    digitalWrite(in2,LOW);//turn off
    digitalWrite(in3,LOW);//turn off
    digitalWrite(in4,LOW);//turn off
    digitalWrite(in5,LOW);//turn off
    digitalWrite(in6,LOW);//turn off
    digitalWrite(in7,LOW);//turn off
    digitalWrite(in8,LOW);//turn off
  }

Oh. Ok. Thanks. Been trying to figure it out for like 5 days straight. Thank you.