Sensor Code!!

Help!!

#include <NewPing.h>

#define TRIGGER_PIN 8 //Trigger send to pin 14 or analog 0:
#define ECHO_PIN 9 //Echo send to pin 15 or analog 1:
#define MAX_DISTANCE 2000 //Maximum read distance of sensor 20 meters:

NewPing DistanceSensor(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); /*assigns the functions of the distance sensor in 
order. You do it here, before the loop so that it is recognised throughout the sketch. If you put it in the loop 
it would disappear after each loop.*/


void setup() {
  #include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

  myservo.attach(9);  Serial.begin(9600); // Sets the baud rate to communicate to the arduino:
  }

void loop(){ 
    for (pos = 0; pos <= 180; pos += 1)  // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  
    for (pos = 180; pos >= 0; pos -= 1)  // goes from 180 degrees to 0 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  unsigned int cm = DistanceSensor. ping_cm(); /* Creates the function called ping_cm from the dist sensor.
  It sends a ping but also returns a result in cm. The unsigned integer means positive (not neg)data will be stored
  in memory where a value can be given (variable*/
  
  Serial.print("Distance: "); //Distance will be written on the first line of the serial monitor:
  Serial.print(cm); // cm will be written after Distance on the first line:
  Serial.println("cm"); // the distance from the sensor in cm will be displayed on a new line with each reading:
  delay(500); }// Each reading will be updated every second

Please read "How to use this forum" and follow the directions.

Hi,
Help!! what help do you ned? I for one am not a mind reader, your supposed to TELL us!! Unless I know what you device is? perhaps some kind of robot? I can't tell if your code works or not... I never use the new/ping.lib you don't need it the code for the U/S is simple...

I use this code:

long scanner(long cm)
{
	const int pingPin=7, EchoPin=8;
	long duration;

	// The SRF005/HC-S04 is triggered by a HIGH pulse of 2 or more microseconds.
	// Give a short LOW pulse before to ensure a clean HIGH pulse:
	pinMode(pingPin, OUTPUT);
	pinMode(EchoPin, INPUT);  

	digitalWrite(pingPin, LOW);
	delayMicroseconds(2);
	digitalWrite(pingPin, HIGH);
	delayMicroseconds(2);
	digitalWrite(pingPin, LOW); 
	duration = pulseIn(EchoPin, HIGH);
	delay(100);

	// convert the time into a distance
	// inches = microsecondsToInches(duration);
	cm = microsecondsToCentimeters(duration);
	return (cm);
}
//------------------------------------------------------------------
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;
}

Hope it helps.
Regards

Mel.