I got a Ping))) sensor from RadioShack yesterday. I just hooked it up to my Arduino/ ProtoShield combo and modded some code so that if an object is within 70cm of the device, it will light up an LED which is tied to DIO pin 13. So, basically, I made a rudimentary motion detector. Once I get my NEW Xbee’s, their breakout boards, and an LCD for my Arudino, I can work on my new wireless motion detector!
Images:
-
-
No pingy!
-
-
Mmm, wires!
-
-
The Ping is pingin’
Code:
int pingPin = 7;
int inConst = 70;
int ledPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if(cm < inConst) {
delay(100);
digitalWrite(ledPin, HIGH);
}
if(cm > inConst) {
digitalWrite(ledPin, LOW);
}
delay(100);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}