'servo' was not declared in this scope

#include <Servo.h>

#define echoPin  2 // define pin 2 on the arduino as echopin ( you can change it to any pin without ~)
#define trigPin  3 //define pin 3 on the arduino as trigpin (you can change it to any pin with ~)


// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int servoPin = 12;


void setup() {
  
  Serial.begin(9600);                                     // Serial Communication is starting with 9600 of baudrate speed
  servo.attach(servoPin); 
  pinMode(trigPin, OUTPUT);                               // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);                                // Sets the echoPin as an INPUT 
  servo.write(0);
  delay(100)
  servo.attach()

}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW); //set the trig pin OFF
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH); //set the trig pin OFF 
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);  //turn it of again
  
  duration = pulseIn(echoPin, HIGH); // turning it ON to read the sound wave travel from the speaker and back in microsecond(μs)
  
  // Calculating the distance
  distance = duration * 0.034 / 2; //  jarak = duration(μs) x kelajuan bunyi(cm/μs)/2



 if ( dist<25 ) { //Change distance as per your need
 servo.attach (servoPin);
  delay(1);
 servo.write(0);  
 delay(500);       
 servo.write(150);    
 delay(100);
 servo.detach();   

 }

  // Print distance to Serial monitor
  Serial.print("Distance: "); 
    if (distance > 110) Serial.println("Out Of Range");
    if (distance < 100) Serial.print(distance);                                                                                                   
    if (distance < 100) Serial.println(" cm"); 
  

  delay(200); //delay for 200 milisecond before doing it again. (can change according to specific)
}

pls help me i dont know what causing the problem and dont be angry at me, im just trying to learn arduino for school project and my other friend didnt want.

You have #included the Servo library but never created a Servo object with the name of servo

Also, is there a reason why you attach() and detach() the servo so many times ?

didn't know, follow some guy online on youtube,his code was exactly like mine but work. also can you edit it for me. thanks in advance

Please post a link to the youtube video that includes the code you used please.

Have you looked at the examples supplied with the IDE for using a servo motor?

can you edit it for me.

I could, but I am not going to.

It would be better for you if you looked at the Servo examples in the IDE as has been suggested and made the changes yourself

    if (distance < 100) Serial.print(distance);                                                                                                   
    if (distance < 100) Serial.println(" cm");

:smiley:

And this is why we have books, and don’t rely on YT.

(OTOH, it does make blatant plagiarism easier to spot)

well im a new coder less than 1 hour experience and i was in rush the last thing in my mind was youtube, nevermind i did it my self my looking at the example. i didnt copy 100% of the youtube video and watch it as reference.

heres mine

#include <Servo.h>
#define echoPin  2 // define pin 2 on the arduino as echopin ( you can change it to any pin without ~)
#define trigPin  3 //define pin 3 on the arduino as trigpin (you can change it to any pin with ~)


Servo servoPin; //created an object for the servo


long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement



void setup() {
 
  Serial.begin(9600);                                     // Serial Communication is starting with 9600 of baudrate speed
  servoPin.attach(12);                                    // attach servo to pin 12
  pinMode(trigPin, OUTPUT);                               // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);                                // Sets the echoPin as an INPUT 
  servoPin.write(0);                                      // set it at position 0 at start
 
  

}
void loop() {
  
  digitalWrite(trigPin, LOW);           //set the trig pin OFF
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);          //set the trig pin OFF 
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);           //turn it of again
  
  duration = pulseIn(echoPin, HIGH);    // turning it ON to read the sound wave travel from the speaker and back in microsecond(μs)
  
  // Calculating the distance
  distance = duration * 0.034 / 2;      //  jarak = duration(μs) x kelajuan bunyi(cm/μs)/2

  if (distance <= 25)                   //servo will spin 180 if the distance is less than 25cm
  servoPin.write(180);

  if (distance <= 25)                   //pause for 1 second if distance is less than 25cm
  delay(1000);

  if (distance >= 25)                   //servo will spin to 0 if the distance is more than 25cm
  servoPin.write(0);
 

  // Print distance to Serial monitor
    Serial.print("Distance: "); 
    if (distance > 110) Serial.println("Out Of Range");
    if (distance < 100) Serial.print(distance);                                                                                                   
    if (distance < 100) Serial.println(" cm"); 
  

  delay(200); //delay for 200 milisecond before doing it again. (can change according to specific)

heres him : Smart Dustbin - Google Drive

btw thanks for the help UKhelibob.
case closed

digitalWrite(trigPin, HIGH); //set the trig pin OFF The hazards of cut-and-paste without comprehension

his code was exactly like mine

The first 2 lines of the code you linked to

#include <Servo.h>   //servo library
Servo servo;

How far into his code did you read ?

TheMemberFormerlyKnownAsAWOL:

  digitalWrite(trigPin, HIGH);          //set the trig pin OFF

The hazards of cut-and-paste without comprehension

i actually write that myself, i didnt know at the time you could change the name of the pin. dont bother to change it since the sensor leg is also named trig. also dont be rude? like i said this account was just made for that problem and i have no idea what coding even is before 3 days ago.i learn it only about 2 hour just now jeez.

UKHeliBob:
The first 2 lines of the code you linked to

#include <Servo.h>   //servo library

Servo servo;



How far into his code did you read ?

well i certainly missed that one.