Hello I'm working on a project for an automatic door and need some assistance

#include <Servo.h>
#include <NewPing.h>
#define TRIG_PIN 8
#define ECHO_PIN 9
#define SERVO_PIN 10
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping for

void setup () {
  Serial.begin(9600);
  Servo.attach(SERVO_PIN);
  NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
}

void loop () {
  unsigned int distance = sonar.ping_cm();
  if (distance < 30) {
   servo.write(180); // rotate servo to 180 degrees
   Serial.print("Object is too close! Distance: ");
   Serial.println(distance);
} else {
   servo.write(0); // rotate servo to 0 degrees
   Serial.print("Object is at safe distance. Distance: ");
   Serial.println(distance);
}

  delay(50);
}

Every time I try to run the code it show's an error saying":\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino: In function 'void setup()':
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino:10:8: error: expected unqualified-id before '.' token
Servo.attach(SERVO_PIN);
^
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino: In function 'void loop()':
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino:15:27: error: 'sonar' was not declared in this scope
unsigned int distance = sonar.ping_cm();
^~~~~
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino:17:4: error: 'servo' was not declared in this scope
servo.write(180); // rotate servo to 180 degrees
^~~~~
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino:17:4: note: suggested alternative: 'Servo'
servo.write(180); // rotate servo to 180 degrees
^~~~~
Servo
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino:21:4: error: 'servo' was not declared in this scope
servo.write(0); // rotate servo to 0 degrees
^~~~~
C:\Users\HP\Documents\Arduino\sketch_may7f\sketch_may7f.ino:21:4: note: suggested alternative: 'Servo'
servo.write(0); // rotate servo to 0 degrees
^~~~~
Servo

exit status 1

Compilation error: expected unqualified-id before '.' token" but I don't know what to do please help

See below the changes/adjustments I have made in you sketch. It is compiled now. You may ask questions why such cahages are needed.

#include <Servo.h>
#include <NewPing.h>
#define TRIG_PIN 8
#define ECHO_PIN 9
#define SERVO_PIN 10
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping for
Servo myServo;
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

void setup ()
{
  Serial.begin(9600);
  myServo.attach(SERVO_PIN);
}

void loop ()
{
  unsigned int distance = sonar.ping_cm();
  if (distance < 30)
  {
    myServo.write(180); // rotate servo to 180 degrees
    Serial.print("Object is too close! Distance: ");
    Serial.println(distance);
  }
  else
  {
    myServo.write(0); // rotate servo to 0 degrees
    Serial.print("Object is at safe distance. Distance: ");
    Serial.println(distance);
  }

  delay(50);
}

It seems I forgot to declare the servo but for the ping sonar I didn't even know how thanks please explain the sonar declaration

Bu that line was in the setup() function of your sketch of post #1. I have just made it global so that the object sonar could be accessed from loop()/any function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.