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