http://www.youtube.com/watch?v=ayDjmjsbGKI&feature=plcp
The ultrasonic range finder I bought has 4 pins instead of commonly found 3 pin model. There is a Trig pin, which is to "trigger" the range finder and an echo pin which gives the distance (elapsed time of the echo). According to the data sheet, if the trig pin remains high for 10 microseconds, it will send short burst of ultrasonic sound, then the echo pin becomes high until it receives the echo. By measuring the time that the echo pin remains high, we can calculate the distance. Pretty straight forward.
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int Trig=11;
int Echo=12;
void setup()
{
pinMode(Trig,OUTPUT);
pinMode(Echo,INPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Distance=");
}
void loop()
{
float duration, distance;
lcd.setCursor(0,1);
digitalWrite(Trig,LOW);
delayMicroseconds(10);
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
duration=pulseIn(Echo,HIGH);
distance=duration/29.0/2.0;
lcd.print(distance);
lcd.print(" ");
lcd.print(distance);
delay(100);
}
No comments:
Post a Comment