#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