Ultrasonic sensor to contol servo motro angle; help

Hi experts,

Need help with following.

This line: myservo.attach(SERV0_PIN); gives me output
either Ping: 0cmAngle: 0 degrees or Ping: 1149cmAngle: 4136 degrees.

When I write myservo.attach(10); or //myservo.attach(SERV0_PIN);

output reading are fine. Very strange. Don’t know what I am missing.
Here is the code

include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
#define SERV0_PIN 8 //
int val; // variable to read the value from the analog pin

void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.

pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

myservo.attach(SERV0_PIN); // attaches the servo
pinMode(SERV0_PIN, OUTPUT);
}

void loop() {
delayMicroseconds(10); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

val= (uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
int valmap = map(val, 0, 50, 0, 180); // scale it to use it with the servo (value between 0 and 180)

Serial.print("Ping: ");Serial.print(val);Serial.print(“cm”);Serial.print( "Angle: “);Serial.print(valmap);Serial.println(” degrees ");

delay(100);
pinMode(SERV0_PIN, OUTPUT);
delay(10);
/this line/ myservo.write(valmap); // sets the servo position according to the scaled value
delay(10);
pinMode(SERV0_PIN, OUTPUT);
delay(100); // waits for the servo to get there defualt 15

}

First up, you should post your code between code tags, not inline.

Now, you say:-

This line: myservo.attach(SERV0_PIN); gives me output
either Ping: 0cmAngle: 0 degrees or Ping: 1149cmAngle: 4136 degrees.

When I write myservo.attach(10); or //myservo.attach(SERV0_PIN);
output reading are fine. Very strange. Don’t know what I am missing.

You have “SERVO_PIN” defined as 8, not 10. Which pin is your servo ‘actually’ attached to?

You have:-

delayMicroseconds(10);   // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

You don’t needthis delay here, since you have the massive delays at the end of loop(). And what is that comment about?

In setup, you don’t need this:-

pinMode(SERV0_PIN, OUTPUT);

‘Servo.attach()’ automatically makes the pin an output.

The same goes for your ultrasonic sensor pins. When you create a NewPing object, the trigger pin is automatically made an output, and the echo pin is automatically made an input.
So you don’t need this:-

pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

And then later you have this:-

delay(100);
  pinMode(SERV0_PIN, OUTPUT);
  delay(10);
 /*this line*/     myservo.write(valmap);                  // sets the servo position according to the scaled value
  delay(10);
  pinMode(SERV0_PIN, OUTPUT);
  delay(100);                              // waits for the servo to get there defualt 15

Why do you repeatedly make ‘SERV0_PIN’ an output? And wrapped in delays for some reason.?

And God only knows why you want that 100mS delay after ‘Serial.print()’

If most of the crap is removed from your code, (and it’s posted correctly between code tags), it looks like this.

include <NewPing.h>
#include <Servo.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define SERV0_PIN 8 //

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Servo myservo;  // create servo object to control a servo

int val;    // variable to read the value from the analog pin

void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  myservo.attach(SERV0_PIN);  // attaches the servo
}

void loop() {
  delayMicroseconds(10);                    // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  val= (uS / US_ROUNDTRIP_CM);       // Convert ping time to distance in cm and print result (0 = outside set distance range)
  int valmap = map(val, 0, 50, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  Serial.print("Ping: ");Serial.print(val);Serial.print("cm");Serial.print( "Angle: ");Serial.print(valmap);Serial.println(" degrees  ");
  delay(100);
 /*this line*/     myservo.write(valmap);                  // sets the servo position according to the scaled value
  delay(100);                              // waits for the servo to get there defualt 15
 }

I left the servo pin error alone. You need to decide which pin your servo is ‘really’ on.
I also left the 10uS delay at the start of the loop() function alone. (I have to leave something for you to do.)
Now read over your code, fix the obvious errors and decide where it’s sensible to use delays. :slight_smile: