(Uno R3)Ultrasonic sensor& servo troubleshooting

Servo myservo; // create servo object to control a servo

const int trigPin = 2;

const int echoPin = 4;

void setup() {

// initialize serial communication:

Serial.begin(9600);

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 7 && cm < 14)

{

myservo.write(140); // sets the servo position according to the scaled value

delay(4000);

}

else if ( cm < 8)

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

else

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

sketch_jun27a:1:1: error: 'Servo' does not name a type; did you mean 'Serial'?

Servo myservo; // create servo object to control a servo

^~~~~

Serial

C:\Users\jjwan\OneDrive\Documents\Arduino\sketch_jun27a\sketch_jun27a.ino: In function 'void setup()':

sketch_jun27a:13:1: error: 'myservo' was not declared in this scope

myservo.attach(9); // attaches the servo on pin 9 to the servo object

^~~~~~~

C:\Users\jjwan\OneDrive\Documents\Arduino\sketch_jun27a\sketch_jun27a.ino: In function 'void loop()':

sketch_jun27a:49:1: error: 'myservo' was not declared in this scope

myservo.write(140); // sets the servo position according to the scaled value

^~~~~~~

sketch_jun27a:59:1: error: 'myservo' was not declared in this scope

myservo.write(40); // sets the servo position according to the scaled value

^~~~~~~

sketch_jun27a:69:1: error: 'myservo' was not declared in this scope

myservo.write(40); // sets the servo position according to the scaled value

^~~~~~~

exit status 1

'Servo' does not name a type; did you mean 'Serial'?

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

@mtgchicken Why do users choose to ignore the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

You need to include the servo library:

#include <Servo.h>
1 Like

Thanks for actually answering my question!! :slight_smile:

Hi,

I'm sorry but a lot more people could have answered, if you posted your code so it can be read easily and without the possibility of text artifacts.
A lot of members would have been turned off from this thread the minute they viewed post #1.

To add code please click this link;

Thanks.. Tom... :smiley: :+1: :coffee: :australia:
PS Your code.

Servo myservo; // create servo object to control a servo

const int trigPin = 2;

const int echoPin = 4;

void setup() {

// initialize serial communication:

Serial.begin(9600);

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 7 && cm < 14)

{

myservo.write(140); // sets the servo position according to the scaled value

delay(4000);

}

else if ( cm < 8)

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

else

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

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