Go Down

Topic: Servo (PAN) and Parallax Ping))) (Read 1 time) previous topic - next topic

cherault

Sep 05, 2012, 05:49 pm Last Edit: Sep 05, 2012, 06:03 pm by cherault Reason: 1
Dear all,

I am writing a small program which permit to do a rangefinder through a Ping sensor which is mounted over a pan servo.
My problem is the next.

I would like to be able to measure distance during the servo movement (0-180°) and i can't !!!

This my small code, could you help me telling me where i could write my FOR loops (from 0-180) and 180-0) during the distance measures ?
I think i can do it using a for loop which include the "void loop()", one for 0-180 and the other for returning from 180 to 0.

Thank you so much

Here is my code (Without the servo code)

#include <Servo.h>


Servo servo1;
const int pingPin = 2;

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
 servo1.attach(8);
 servo1.write(90);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, cm;

 // 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);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 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);

 // convert the time into a distance

 cm = microsecondsToCentimeters(duration);

 Serial.print(cm);
 Serial.print("cm");
 Serial.println();

 delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}
50% homme + 50% machine = 100% linux

AWOL

First, post code in code tags to avoid smileys.

Second, break out the Ping code into a simple function returning the range in your desired units - it'll make you code cleaner and easier to debug.
I must've posted the simple function a dozen times here.

Disclaimer: I don't own a Ping and have never used one.

PaulS

Quote
I would like to be able to measure distance during the servo movement (0-180°) and i can't !!!

Possibly because you never move the servo in loop().

Quote
I think i can do it using a for loop which include the "void loop()", one for 0-180 and the other for returning from 180 to 0.

What's the problem when you do that?

Quote
could you help me telling me where i could write my FOR loops (from 0-180) and 180-0) during the distance measures ?

You don't do the for loop during the distance measures. You do the distance measures in the for loop.
The art of getting good answers lies in asking good questions.

jraskell

Code re-use is a wonderful thing:
http://arduino.cc/forum/index.php/topic,106043.0.html

Go Up