Hi Ken,
Here's the code:
#define trig 3
#define echo 2
#define led 1
void setup() {
pinMode(trig, OUTPUT);// attach pin 3 to Trig
pinMode (echo, INPUT);//attach pin 2 to Echo
pinMode(led, OUTPUT);//attach pin 1 to LED
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, 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:
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, 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.
duration = pulseIn(echo, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if(inches < 4){
digitalWrite(led, HIGH); // set the LED on
delay(1000); // wait for a second // wait for a second
} else if (inches>=4){
digitalWrite(led,LOW);
}
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
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;
}
I don't understand what you mean by "reference pin for ADC" or the rest, unfortunately. On the Arduino platform I understand "plug line A into port A" and read the output in the code. As a software guy, it works and that's great but the physical mass of even the smallest Arduino form factor does not work for my project. I know I'm asking annoying noob questions and am loathe to invite a teachable moment, but if you could explain what you mean I would appreciate it, and perhaps through Google many others in my shoes would appreciate it too.