Go Down

Topic: WHY IT IS COMING? (Read 260 times) previous topic - next topic

psk_villa123456

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

my code =
Code: [Select]

#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);
}
}

AWOL

#1
Oct 11, 2018, 03:05 pm Last Edit: Oct 11, 2018, 03:06 pm by AWOL
What else did it say?

Code: [Select]
servo.write(140);Outside a function
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

psk_villa123456

exit status 1
'servo' does not name a type


AWOL

Even after you put the code that was outside the loop function into the loop function?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

johnwasser

Code: [Select]
 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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

psk_villa123456


AWOL

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"
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

vinceherman

#7
Oct 11, 2018, 05:21 pm Last Edit: Oct 11, 2018, 05:22 pm by vinceherman
You have a lot of mismatch curly braces.
Your loop function ends at line 46.  Everything else after that is causing errors.



bidouilleelec

Hello psk_villa123456
I CAN NOT UNDERSTAND
You should have a ( a lot of ) look at all "}".

Regards,
bidouilleelec

TomGeorge

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... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up