error: expected unqualified-id before 'if' | Anyone can help me??

Hello, I have an error anyone can help me??

const int PWM_A_Pin = 5;
const int Motor_A_Pin1 = 6;
const int Motor_A_Pin2 = 7;
int F1=0; //Final de carrera 1
int F1ant=0; //Estado anterior del F1.
int F2=0; //Final de carrera 2
int F2ant=0; //Estado anterior del F2.
int P1=0; //Pulsador 1
int P1ant=0; //Estado anterior del P1.
int cerrado=0; //Esperando a que se pilse el interruptor.
int abriendo=0; //Abriendo la puerta.
int abierto=0; //Ha llegado al final de carrera 1 y se h a parado.
int cerrando=1; //Se esta cerrando la puerta.
int paro=1000;
int velocidad=255;

void setup(){
pinMode(8, INPUT); //F1
pinMode(9, INPUT); //F2
pinMode(13, INPUT); //P1
pinMode(Motor_A_Pin1, OUTPUT);
pinMode(Motor_A_Pin2, OUTPUT);
pinMode(PWM_A_Pin, OUTPUT);
}

void loop(){

P1=digitalRead(13);
F1=digitalRead(8);
F2=digitalRead(9);

if(P1==HIGH && P1ant==LOW)  {   
cerrado=0;
abriendo=1;
delay(20);                  
}  
if(abriendo==1) digitalWrite(P1,HIGH);
else {digitalWrite(P1, LOW);
}

if (F1==HIGH && F1ant==LOW) {
abriendo=0;
abierto=1;
delay(20); 
}
if(abierto==1) digitalWrite(F1,HIGH);
else{digitalWrite(F1, LOW);
}

if (P1==HIGH && P1ant==LOW){
abierto=0;
cerrando=1;
delay(20);
}  
if(cerrando==1) digitalWrite(P1,HIGH);
else{digitalWrite(P1, LOW);
}

if (F2==HIGH && F2ant==LOW){
cerrando=0;
cerrado=1;
delay(20); 
} 
if(cerrado==1) digitalWrite(F2,HIGH);
else{digitalWrite(F2, LOW);
}

} 
if (abierto==1){
digitalWrite(Motor_A_Pin1,LOW);
digitalWrite(Motor_A_Pin2,LOW);
delay(paro);
}
if (cerrado==1){
digitalWrite(Motor_A_Pin1,LOW);
digitalWrite(Motor_A_Pin2,LOW);
delay(paro);
}
if (abriendo==1){
analogWrite(PWM_A_Pin, velocidad);
digitalWrite(Motor_A_Pin1,LOW);
digitalWrite(Motor_A_Pin2,HIGH);
}
if (cerrando==1){
analogWrite(PWM_A_Pin, velocidad);
digitalWrite(Motor_A_Pin1,HIGH);
digitalWrite(Motor_A_Pin2,LOW);
}

Some of the if tests are outside of loop (ie, you have an extra closing brace in the middle).

Use ctrl+T autoformat to make it really obvious where (though the compiler tells you the line number!)

Read the "how to use this forum" post before you start a new thread; it tells you about using code tags (like crossroads edited in for you) and posting complete error messages and stuff like that.

Sure, I added code tags for you. Next post, you do it - use the </> button on the menu.

The last } here

if(cerrado==1) digitalWrite(F2,HIGH);
else{digitalWrite(F2, LOW);
}

}

I think closes out loop(), so the compiler is complaining about code being outside of loop, and not being in another function.
Delete that } and add one after the last if section.

Thanks to all