Author Options:

URGENT!!!sketch for arduino robot with ping? Answered

i am making an arduino obstacle avoiding robot for my science project.. But i am not getting a suitable sketch for a ping to use with it. I am only getting that of sharp ir sensor. Please help me out with a sketch..its urgent..below i have linked the site where there is the sketch and the project dat i want to make.....




4 years ago

(im assuming you are using a PING))) sonar module)

Ok this isn't that difficult. The idea is to replace the normal value retrieved from the IR-sensor and substitute it with a value retrieved from the PING. So we grab a existing Ping-reading code and alter the Dist variable to take the value from the PING code instead of the analog sensor.

On the arduino site is a easy PING))) tutorial you can use for your sensor.


If you copy the code of the tutorial loop, put it in place at the beginning of the sketch's loop, use the math of either Convert to <inches/cm> functions to put a understandable value in place of dist and finally adjust the object value to reflect the change you should get that the sketch will work with the Ping))) instead.

So in steps:

1) in your Sketch declare the pingPin to whatever you are using(can be any i/o pin).

2) Copy the tutorial's main loop up to duration=Pulsein.... (this code gives a vallue in duration.

2) Put that code in your own sketch loop right before dist=analogread(sensorpin);

3) Change Dist=AnalogRead(pin) to Dist=duration/29/2; (this gives Dist a vallue in centimeters based on the value read from the Ping code. we steal this formula from the tutorial's Convert to cm function.)

4) Change object variable (normally 500) to something in cm...like 50

Hope this helps and isn't confusing you.


Answer 4 years ago

Example i quickly cobbled up:

So instead of:

void loop()

You could put:

void loop()
long duration;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);


digitalWrite(pingPin, HIGH);


digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH

// pulse whose duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

pinMode(pingPin, INPUT);

duration = pulseIn(pingPin, HIGH);
dist=duration/29/2; //convert microsecond to range in centimeters

Just dont forget to declare pingPin beforehand at the top of the sketch and to change the object variable to a range in cm.

const int pingPin = 7; //Change pingPin to pin Digital pin you are using

4 years ago

So in ALL the examples like the ones on the right, there is NOTHING to do with Ping ? Like this ?