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;
}