Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ultrasonic range finder using Amarino Evaluation shield
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 10, 9, 8, 7);
int TrigPin = 4;
int EchoPin = 2;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
}
void loop()
{
int distance,duration;
digitalWrite(TrigPin,HIGH);//TrigPin prepare high of more than 10us
delayMicroseconds(11);
digitalWrite(TrigPin,LOW);
duration = pulseIn(EchoPin, HIGH);//EchoPin received high start counting until the receiver to the low,return to the count valu
duration = duration/29/2;//Calculating the distance cm
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
lcd.clear();
lcd.print("Distance:");
lcd.print(duration);
Serial.print(duration);//Serial display distance
lcd.print("cm");
Serial.print("cm");
Serial.println();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment