WHY IT IS COMING?

please help when i am compiling code i get =>exit status 1
‘servo’ does not name a type

my code =

#include <Servo.h>
#define buzzer 3
#define trigPin 5
#define echoPin 6
Servo servo;
int sound = 250;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin (9600);
  servo.attach (9);
  servo.write(90);
}
void loop() {
  // put your main code here, to run repeatedly:
  long duration, distance;
  digitalWrite (trigPin, LOW);
  delayMicroseconds (2);
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  servo.write (115);
  if (distance < 10 && distance > 5)
  {
    Serial.println("the distance is less than 10");
    digitalWrite (buzzer, HIGH);
    delay(500);
  }
  else
  {
    digitalWrite (buzzer, LOW);
  }
  if (distance > 60 || distance <= 0)
  {
    Serial.println("The distance is more than 60");
  }
  else
  {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}
servo.write(140);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}
servo.write(165);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}
servo.write(90);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}

servo.write(75);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}
servo.write(50);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}
servo.write(25);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}
servo.write(0);
if (distance < 10 && distance > 5)
{
  Serial.println("the distance is less than 10");
  digitalWrite (buzzer, HIGH);
  delay(500);
}
else
{
  digitalWrite (buzzer, LOW);
}
if (distance > 60 || distance <= 0)
{
  Serial.println("The distance is more than 60");
}
else
{
  Serial.print(distance);
  Serial.println(" cm");
}
delay(500);
}
}

What else did it say?

servo.write(140);Outside a function

exit status 1
'servo' does not name a type

Even after you put the code that was outside the loop function into the loop function?

  delay(500);
}   <- the loop() function ends here
servo.write(140);

You can only have declarations outside of functions and declarations start with a type name. The name ‘servo’ is not the name of a type.

Common compiler errors caused by mismatched brackets:

“does not name a type” or
“expected declaration before” or
“expected unqualified-id before”
Usually means you forgot a ‘{’ or put in an extra ‘}’ in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs ‘{’ followed by ‘}’.

“a function-definition is not allowed here before ‘{’ token”
(can cause: “‘functionName’ was not declared in this scope”)
Usually means you forgot a ‘}’ or put in an extra ‘{’ in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can’t declare a function inside a function so if the compiler finds a function declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs ‘{’ followed by ‘}’.

“expected ‘}’ at end of input”
Usually means you forgot a ‘}’ or put in an extra ‘{’ in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead it emits an error. Make sure that the brackets in the last function are in matching pairs ‘{’ followed by ‘}’.

“expected primary-expression before ‘}’ token”
Usually means you have an incomplete statement before a ‘}’. The block statement (between ‘{’ and matching ‘}’) can only contain complete statements.

I CAN NOT UNDERSTAND

Ok, there is no need to SHOUT.

You have code that is not in a function.
The C language does not allow this.
The code has to be put into a function.
That function should probably be "loop"

You have a lot of mismatch curly braces.
Your loop function ends at line 46. Everything else after that is causing errors.

psk_villa123456:
I CAN NOT UNDERSTAND

Language problem???
Try language closer to native language:
中文 - Chinese
Deutsch - German
Español - Spanish
Français - French
অসমীয়া, বাংলা, बड़ो (India)
Italiano - Italian
Nederlands - Dutch
Português - Portuguese
Русский - Russian
Svenska - Swedish, Suomi - Finnish, Norsk - Norwegian

Hello psk_villa123456

psk_villa123456:
I CAN NOT UNDERSTAND

You should have a ( a lot of ) look at all "}".

Regards,
bidouilleelec

Hi,
With your code in the IDE, select TOOLS then Auto Format.
It will detent your brackets so you can see if they are all in the right place.
You should have as many { as } brackets.

Tom... :slight_smile: